Search code examples
flutterdartroutesflutter-go-router

Flutter go_router parameters within ShellRoute not working


I am using go_router and need have setup a ShellRoute. It is working fine so far. Now one of the routes inside that ShellRoute should have a route with params. And I can not make it work..

This is my setup:

final rootNavigatorKey = GlobalKey<NavigatorState>();

class AppRouter {
  static final _shellNavigatorKey = GlobalKey<NavigatorState>();

  static final router = GoRouter(
    initialLocation: IntroView.path,
    debugLogDiagnostics: true,
    navigatorKey: rootNavigatorKey,
    routes: [
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        pageBuilder: (context, state, child) {
          return NoTransitionPage(
            child: ScaffoldView(
              initLocation: state.location,
              child: child,
            ),
          );
        },
        routes: [
          GoRoute(
            name: ProjectsView.name,
            path: ProjectsView.path,
            parentNavigatorKey: _shellNavigatorKey,
            pageBuilder: (context, state) {
              return const FadePageTransition(
                page: ProjectsView(),
              );
            },
            routes: [
              GoRoute(
                parentNavigatorKey: rootNavigatorKey,
                path: 'projects/:projectTitle',
                pageBuilder: (context, state) {
                  return FadePageTransition(
                    page: ProjectDetailScaffold(
                      project: Projects.values.byName(
                        state.params['projectTitle']!,
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
          ...

And I am trying to push to ProjectDetailScaffold like this:

rootNavigatorKey.currentContext!.push(
              '/projects/wishlists',
            );

But I get an error that no route exists for /projects/wishlists...

What am I missing here?

Let me know if you need any more info.


Solution

  • I figured it out by myself:

    The problem was that I specified path of the subRoute wrong. I added it's parent path as well when I only need to add :projectTitle. Here is the working solution:

      GoRoute(
        parentNavigatorKey: rootNavigatorKey,
        path: ':projectTitle',
        pageBuilder: (context, state) {
          return FadePageTransition(
            page: ProjectDetailScaffold(
              project: Projects.values.byName(
                state.params['projectTitle']!,
              ),
            ),
          );
        },
      ),