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

question: passing bloc/cubit in sub route using gorouter #4380

Closed
PrzemyslawPluszowy opened this issue Mar 21, 2025 · 2 comments
Closed

question: passing bloc/cubit in sub route using gorouter #4380

PrzemyslawPluszowy opened this issue Mar 21, 2025 · 2 comments
Assignees

Comments

@PrzemyslawPluszowy
Copy link

PrzemyslawPluszowy commented Mar 21, 2025

Hello, I have a question about go_router and Bloc/Cubit.

I have example route structure

       | 
       │ ─/products 
       |─/emails.   <<< CrateMultiBlocProvider
       │      ─/emails/emailDetail.     <<< how to get context in sub route  with existing instance
       │      ─/emails/emailEvent.    <<<. how to get context in sub route. with existing instance

How can I insert MultiBlocProvider in a route and get context in a sub-route? go_router creates a new path and context in the sub-route, but I am referring to app/main.

For example, somewhere in the routing, I create a MultiBlocProvider and initialize new blocs that I want to be closed when leaving this route. However, I also want the context to be passed down to the sub-routes.

@PrzemyslawPluszowy
Copy link
Author

i try like this but don't work:

Image

@PrzemyslawPluszowy PrzemyslawPluszowy changed the title question: passing bloc/cubit in sub route using grouter question: passing bloc/cubit in sub route using gorouter Mar 21, 2025
@Gene-Dana
Copy link
Collaborator

Hi there ! I believe you are looking for the shell route feature of Go Router

https://pub.dev/documentation/go_router/latest/go_router/ShellRoute-class.html

    navigatorKey: _rootNavigatorKey,
    initialLocation: '/a',
    routes: [
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
/// this is where you can provide blocs/cubit where the context is shared
        builder: (context, state, child) {
          return ScaffoldWithNavBar(child: child);
        },
        routes: [
          // This screen is displayed on the ShellRoute's Navigator.
          GoRoute(
            path: '/a',
            builder: (context, state) {
              return const ScreenA();
            },
            routes: <RouteBase>[
              // This screen is displayed on the ShellRoute's Navigator.
              GoRoute(
                path: 'details',
                builder: (BuildContext context, GoRouterState state) {
                  return const DetailsScreen(label: 'A');
                },
              ),
            ],
          ),
          // Displayed ShellRoute's Navigator.
          GoRoute(
            path: '/b',
            builder: (BuildContext context, GoRouterState state) {
              return const ScreenB();
            },
            routes: <RouteBase>[
              // Displayed on the root Navigator by specifying the
              // [parentNavigatorKey].
              GoRoute(
                path: 'details',
                parentNavigatorKey: _rootNavigatorKey,
                builder: (BuildContext context, GoRouterState state) {
                  return const DetailsScreen(label: 'B');
                },
              ),
            ],
          ),
        ],
      ),
    ],

@Gene-Dana Gene-Dana self-assigned this Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants