stacked icon indicating copy to clipboard operation
stacked copied to clipboard

[feature]: Lazy construct RouteGuards

Open FilledStacks opened this issue 1 year ago • 0 comments

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 avatar Jun 17 '24 13:06 FilledStacks