Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.58k stars 402 forks source link

Can't push routes using AutoRouterDelegate.declarative #537

Closed Fiyiin closed 3 years ago

Fiyiin commented 3 years ago

I'm following the authentication example in the docs. The problem is that when I try to push from any of the routes I get this error _AssertionError ('package:auto_route/src/router/controller/routing_controller.dart': Failed assertion: line 696 pos 7: '!managedByWidget': Pages stack can be managed by either the Widget (AutoRouter.declarative) or the (StackRouter))

This is my MaterialApp.router

MaterialApp.router(
  routeInformationParser: _appRouter.defaultRouteParser(),
  routerDelegate: AutoRouterDelegate.declarative(_appRouter, routes: (_) {
     return [
      if (AppModel().isLoggedIn)
         NavigationHandlerRoute()
      else
        LoginRoute()
              ];
         }),
);

Router

@AdaptiveAutoRouter(
  replaceInRouteName: 'Screen,Route',
  routes: <AutoRoute>[
    AutoRoute(page: LoginScreen, path: 'login'),
    AutoRoute(page: SignUpScreen, path: 'sign-up'),
    AutoRoute(
      path: '/',
      page: NavigationHandlerScreen,
      children: [
        AutoRoute(
          path: 'home',
          name: 'HomeRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: HomeScreen)],
        ),
        AutoRoute(
          path: 'notifications',
          name: 'NotificationsRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: NotificationsScreen)],
        ),
        AutoRoute(
          path: 'watching',
          name: 'WatchingRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: WatchingScreen)],
        ),
        AutoRoute(
          path: 'partners',
          name: 'PartnersRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: PartnersScreen)],
        )
      ],
    )
  ],
)
theweiweiway commented 3 years ago

Hey @Fiyiin , how are you pushing the routes? Where are you pushing from and where are you pushing to?

A resource that might be valuable is: https://github.com/flutter/uxr/blob/master/nav2-usability/scenario_code/lib/sign-in-routing/sign_in_routing_auto_route.dart

It uses the AutoRouterDelegate.declarative for auth, and it also works when pushing routes

Fiyiin commented 3 years ago

@theweiweiway from the Login screen to the SignUp screen, I use context.router.push(SignUpRoute). That's where I get the error, when using AutoRouterDelegate.declarative

theweiweiway commented 3 years ago

Ah i see. You have 3 distinct routers at the root level:

LoginScreen router SignUpScreen router NavigationHandlerScreen router

but your routing declaration only has 2:

      if (AppModel().isLoggedIn)
         NavigationHandlerRoute()
      else
        LoginRoute()

you're trying to go from LoginScreen to SignUpScreen but SignUpScreen is not declared in the delegate. What you will need to do is:

@AdaptiveAutoRouter(
  replaceInRouteName: 'Screen,Route',
  routes: <AutoRoute>[
    // notice how combine  login and sign-up into one nested page
    AutoRoute(page: EmptyRouterPage, path: '/auth', 
        name: 'AuthRouter',
        children: [
          AutoRoute(page: LoginScreen, path: 'login'),
          AutoRoute(page: SignUpScreen, path: 'sign-up'),
    ]),
    AutoRoute(
      path: '/',
      page: NavigationHandlerScreen,
      children: [
        AutoRoute(
          path: 'home',
          name: 'HomeRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: HomeScreen)],
        ),
        AutoRoute(
          path: 'notifications',
          name: 'NotificationsRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: NotificationsScreen)],
        ),
        AutoRoute(
          path: 'watching',
          name: 'WatchingRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: WatchingScreen)],
        ),
        AutoRoute(
          path: 'partners',
          name: 'PartnersRouter',
          page: EmptyRouterPage,
          children: [AutoRoute(path: '', page: PartnersScreen)],
        )
      ],
    )
  ],
)

Now, you can do

      if (AppModel().isLoggedIn)
         NavigationHandlerRoute()
      else
        AuthRouter()

Now login and sign-up are both exposed in the delegate, instead of just the login page

Fiyiin commented 3 years ago

thanks a lot @theweiweiway worked with a little modification AutoRoute(page: LoginScreen, path: 'login', initial: true),

EdoardoPi commented 3 years ago

I've got the same problem. I tried to change the parents into the Router, but the issue is still there. Any adivce ?