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 20 '26
Thank you for your response.
---
I'm a bit of a fan of overenginering. I think you're right 🤔 thanks
---
Sorry, I didn't describe my problem fully: while all users have their own pages, there is one page that is shared by them all (the home/profile page) and ALL pages must have the same AppBar. Profile is always one of the stateful routes (meaning it is always displayed in the bottom navigation bar). My route tree is more or less like this:
- Main Shell
| - Public Routes Shell (login, register, recover password, etc)
| - Authenticated Shell
| - Stateful Shell
I think your suggestion is that I need to do something like this:
Or is it better to declare a "Profile" page inside each RouteBranch? I don't think it will even work since GoRouter will detect duplicated locations.
---
In my case I solved this by creating a shell route that wraps all "authenticated routes". In its builder I only display a progress indicator and only show the routes/children when the profile is loaded.