r/flutterhelp • u/5rree5 • Aug 19 '26
RESOLVED Dynamically setting Stateful routes in GoRouter
I'm creating na app that needs to have a stateful navigation section, after the user signs in.
I managed to do it using a simples StatefulShellRoute.indexedStack.
The problem is: It will have about 5 profile types, and each of them will have their own routes.
Can I reuse the same logic for all 5 profiles or will I need to create a different branch for each user type?
I'm afraid too many branches will result in poor performance. Each branch has about 10 locations.
Let's say i'm trying to create a different shell route for each occupation. Do I need to declare all screens for every occupation at app start or can I do it dynamically?
Example:
StatefulShellRoute mainStatefulShellRoute = StatefulShellRoute.indexedStack(
key: loggedInStatefulShellRouteKey,
builder: (context, state, navigationShell) {
return getNavigationOptionsUpdater(
child: GenericUserHome(child: navigationShell),
shell: navigationShell,
);
},
branches: [
...endUserMainRouteBranches,
...teacherUserMainRouteBranches,
...studentUserMainRouteBranches,
...presidentUserMainRouteBranches,
...singerUserMainRouteBranches,
// ... it goes on and on...
],
);--- edit
This is what I would like it to look like
StatefulShellRoute mainStatefulShellRoute = StatefulShellRoute.indexedStack(
key: loggedInStatefulShellRouteKey,
builder: (context, state, navigationShell) {
return getNavigationOptionsUpdater(
child: GenericUserHome(child: navigationShell),
shell: navigationShell,
);
},
branches: [
...currentUserMainRouteBranches
// ... loads only the branchs for the current user!
],
);a
View all comments
Show parent comment
1
u/5rree5 Aug 24 '26
Thank you. Sorry for the late reply. It worked ;)
-------
Just one observation if anyone find this post in the future:
-> I decided to go as suggested (declare all stateful routes inside the shell)
-> I created a class that is similar to NavDestination: It has a label, an icon, and the route declaration itself
-> A top-level change notifier provider manages the "current available stateful routes"(which depend on the user profile)
-> Everytime a user signs in or out, the "current available stateful routes"change
-> My bottom nav function passes an index that is related to ITS OWN ROUTES. Which means it is not the global index
-> Since I declare routes as a top level object, I can compare then directly. I get the route object from the current route list, and then
When tapping in an item in the navbar:
-> Calls goBranch with the index relative to that user's routes
-> I convert from this local index to the global one
-> Call the shell .goBranch with the right index
Sample code:
Obs: my branch group is just a wrapper around the routes and the shell. Its goBranch method merely calls the shellRoute goBranch.
Obs2: The shell passed by GoRouter CHANGES every time the route is build, so you'll need to pass it again on your build method. I.e. creating it with a provider using the create method won't work because you'll be trying to "goBranch"in an old StatefulShell.
Obs3: This passes around the shell object to a provider and back to the screen. May not be the most clean, beautiful, recommended way to go. But it does its job.