Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]: Lazy construct RouteGuards #1108

Open
FilledStacks opened this issue Jun 17, 2024 · 0 comments
Open

[feature]: Lazy construct RouteGuards #1108

FilledStacks opened this issue Jun 17, 2024 · 0 comments
Labels
new feature Requires planning and detailed comments of what will be done to build this

Comments

@FilledStacks
Copy link
Contributor

FilledStacks commented Jun 17, 2024

Is your feature request related to a problem? Please describe.

Yes.

When supplying a StackedRouteGuard to routes in app.dart

class TestGuard extends StackedRouteGuard {
   final databaseService = locator<DatabaseService>(); // <=== This breaks when running for mobile apps
  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) {}
}

@StackedApp(
  routes: [
    CustomRoute(page: StartupView, initial: true),
    CustomRoute(page: MainView, guards: [TestGuard]),
// @stacked-route

    CustomRoute(page: UnknownView, path: '/404'),

    /// When none of the above routes match, redirect to UnknownView
    RedirectRoute(path: '*', redirectTo: '/404'),
  ],
 ....
)
class App {}

It generates the router as follows

final stackedRouter = StackedRouterWeb(
  navigatorKey: _i6.StackedService.navigatorKey,
  testGuard: _i7.TestGuard(),
);

If you run on web this works.
When you run it on mobile apps this break because the router is constructed before the locator code is run.

Describe the solution you would like

The generator should instead generate something like this:

final stackedRouter = StackedRouterWeb(
  navigatorKey: _i6.StackedService.navigatorKey,
  testGuardBuilder: () => _i7.TestGuard(),
);

Which would be built when the page is accessed

Additional Context

No response

@FilledStacks FilledStacks changed the title [feature]: RouteGuards should be dynamically constructed [feature]: Lazy construct RouteGuards Jun 17, 2024
@FilledStacks FilledStacks added the new feature Requires planning and detailed comments of what will be done to build this label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Requires planning and detailed comments of what will be done to build this
Projects
None yet
Development

No branches or pull requests

1 participant