Search code examples
androidiosflutterdartflutter-go-router

GoRouter passing wrong arguments to the route when I press on `Select Widget` in flutter inspector


I have this route with two nested routes:

          GoRoute(
            parentNavigatorKey: _homePageShellNavigatorKey,
            name: MenuScreen.routeName,
            path: '/menu_screen',
            pageBuilder: (_, state) {
              assert(
                state.extra is MenuScreenArgs || state.extra == null,
                'extra of menu_screen route should be null or have a type of MenuScreenArgs,'
                ' but instead got ${state.extra.runtimeType}',
              );
              return MaterialPage(
                child: MenuScreen(
                  args: state.extra as MenuScreenArgs?,
                ),
              );
            },
            routes: [
              GoRoute(
                parentNavigatorKey: _rootNavigatorKey,
                name: CoffeeDetailsScreen.routeName,
                path: 'coffee_details_screen',
                pageBuilder: (_, state) {
                  assert(
                    state.extra != null &&
                        state.extra is CoffeeDetailsScreenArgs,
                    'extra of coffee_details_screen route should have a type of'
                    ' CoffeeDetailsScreenArgs,'
                    ' but instead got ${state.extra.runtimeType}',
                  );
                  return MaterialPage(
                    child: CoffeeDetailsScreen(
                      args: state.extra as CoffeeDetailsScreenArgs,
                    ),
                  );
                },
              ),
              GoRoute(
               // ... another nested route
              ),
            ],
          ),
  • If I am in, say home screen, and go to menu_screen, then the page builder of menu_screen's route is called and the assertion passes with no errors
  • If I am in menu_screen and call this code, then the coffee_details_screen is successfully pushed, but the pageBuilder of both menu_screen and coffee_details_screen are invoked:
                CoffeeDetailsScreen.routeName,
                extra: CoffeeDetailsScreenArgs(productId: product.id),
              );
  • If I am now in the coffee_details_screen, and I open flutter devtools and press on select widget, and I also put breakpoints on the 'pageBuilder's of menu_screen and coffee_details_screen, then I notice that they are both called, but when menu_screen builder is called, it is passed wrong args, it is passed the args of coffee_details_screen which make the assert fail:
  assert(
                state.extra is MenuScreenArgs || state.extra == null,
                'extra of menu_screen route should be null or have a type of MenuScreenArgs,'
                ' but instead got ${state.extra.runtimeType}',
              );

Why is this happening, is it a bug or something wrong in my usage of the router?


Solution

  • Turned out to be a bug in the library itself. So it will be resolved once the maintainer fixes it in the package. https://github.com/flutter/flutter/issues/125900