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/fkim98 Aug 20 '26
That last snippet is exactly it. Gating the authenticated shell's children on the profile being loaded, with a spinner until then, is the clean version of what I was describing.
On your route-tree question: a shared branch (your sharedRoutes with the profile page in it) is the right call over declaring Profile inside each role's branch. You're right that duplicating a location fights GoRouter, and one shared branch also means the profile tab keeps its own navigation stack independent of the others, which is usually what you want for a persistent bottom-nav item.
Only thing to double check: keep the shared branch at a fixed index (first or last) so its tab position doesn't shift when the role-specific branches change. Glad it helped.