Sub topic 14
Pitfalls and Debugging Tips for StatefulShellRoute
This chapter assumes you know Dart and Flutter widgets, understand Navigator stacks, and have basic experience with GoRouter, GoRoute, and context.go. The examples use a shell with multiple tab branches.
StatefulShellRoute keeps a separate Navigator for each StatefulShellBranch. Most bugs come from accidentally changing which navigator owns a route, using paths that do not match the route tree, or rebuilding the shell in a way that loses state.
Check the route tree first
A branch has a root route and can contain nested child routes. A child route uses a relative path:
- Correct:
GoRoute(path: 'details/:id', ...) - Usually incorrect:
GoRoute(path: '/details/:id', ...)
The relative path becomes part of its parent's location. For example, a child of /home with the path details/:id produces /home/details/42.
Do not give two sibling routes the same path. Also check that every tab has a distinct top-level location, such as /home and /settings. Duplicate paths can make matching and redirects appear unpredictable.
Understand navigator ownership
A shell normally has a root navigator and one navigator for each branch. A route placed inside a branch appears in that branch's stack. It keeps the shell UI, such as the bottom navigation bar, visible.
A route with parentNavigatorKey: rootNavigatorKey is placed on the root navigator instead. This is useful for a full-screen photo viewer, login page, or modal-like flow that should cover the tab bar.
A missing or incorrect key can cause an assertion, a route to appear in the wrong place, or a route to disappear when the user changes tabs. Define the root key once and use the same instance in both GoRouter and the route that should live above the shell.
Diagnose unexpected state loss
StatefulShellRoute.indexedStack preserves each branch's navigator. If a user opens /home/details/42, switches to Settings, and returns to Home, the details route should still be present.
State can still be lost when:
- The
GoRouterinstance is recreated during a widget rebuild. - A branch's navigator key changes.
- The app uses
ShellRouteinstead ofStatefulShellRoute. - Code calls
go('/home')when it intended to switch branches. - A custom container removes branch children instead of keeping them mounted.
When debugging, log the current location, navigationShell.currentIndex, and the selected branch. Confirm that switching tabs calls goBranch. Use goBranch(index, initialLocation: true) only when tapping the already selected tab should reset that branch to its initial route.
Redirects and route guards
Redirects can run at the router level or on individual routes. A guard inside a branch should return a location that exists in the route tree. For example, an unauthenticated detail page might redirect to /login.
Avoid redirect loops. If /login redirects to /home while /home redirects back to /login, navigation never settles. Check authentication state before returning a redirect, and return null when the current location is allowed.
When a redirect targets a route above the shell, make sure that route belongs to the root route tree. A branch cannot navigate to a route that is not declared in its accessible tree.
A debugging map
A practical checklist
| Symptom | Likely cause | First check |
|---|---|---|
| Tab content resets | Router or branch key was recreated | Keep them stable |
| Detail page replaces the tab shell | Route uses the root navigator | Remove or change `parentNavigatorKey` |
| Full-screen page keeps the tab bar | Route belongs to a branch navigator | Set the root `parentNavigatorKey` |
| Tab tap does nothing | Wrong branch index | Compare `currentIndex` with destinations |
| Redirect repeats forever | Guard returns a location that redirects back | Test every redirect condition |
Exercise
A user opens a detail page in Home, switches to Settings, and returns to Home. The detail page is gone. Name two likely causes and one debugging step.
Show answer
The app may use ShellRoute, recreate GoRouter, or recreate a branch navigator key. Check that StatefulShellRoute.indexedStack is used and that the router and keys remain stable.