Sub topic 9
How each tab keeps its own stack and state
Assume you know basic Flutter widgets, Navigator, declarative routing, and go_router locations. This chapter focuses on what happens after you create a StatefulShellRoute: why each tab remembers its position, and what can accidentally remove that memory.
Two kinds of state are preserved
A StatefulShellRoute.indexedStack creates a separate Navigator for each StatefulShellBranch. Each navigator owns a route stack for one tab.
For example, suppose the Home branch contains:
/home/home/details/42
If the user opens the details page and then switches to Settings, the Home navigator still has both routes. When the user returns to Home, the details page appears again.
The branch also keeps its widget subtree mounted. This preserves local widget state such as:
- A scroll position
- Text entered into a form
- The selected item in a list
- An animation or focus state
The state stays available while the router and its branch navigators remain alive. This is in-memory preservation. It does not automatically survive process termination, operating-system removal of the app, or a full application restart. Long-term data needs persistence, such as a database or shared preferences.
Why `indexedStack` matters
The default StatefulShellRoute.indexedStack displays one branch at a time while keeping the other branch navigators in the widget tree. Conceptually, it behaves like this:
The shell changes the visible index instead of replacing the inactive branch with a new widget. Therefore, switching tabs is different from navigating to a completely new page.
| Action | Effect on branch state |
|---|---|
| Switch with `navigationShell.goBranch(1)` | Shows the Settings branch and keeps Home mounted |
| Push a detail route inside Home | Adds a route to Home's own stack |
| Tap Home again with `initialLocation: true` | Returns Home to its initial route |
| Recreate the entire router | Usually discards the old branch navigators and their state |
Preserve state when customizing the shell
If you use the regular indexedStack constructor, state preservation is handled for you. Be more careful when using navigatorContainerBuilder.
A custom container must keep every child navigator mounted. An IndexedStack or a set of Offstage widgets works well. A container that returns only children[currentIndex] removes inactive navigators from the tree. When the user returns to a removed branch, Flutter may build it again, losing its widget state and sometimes its navigation stack.
Keep the GoRouter instance stable as well. Do not create a new router inside a widget's build method. Rebuilding the router creates new navigation objects and can reset every branch.
Common state-loss mistakes
Replacing tab switching with unrelated navigation
Use navigationShell.goBranch(index) for tab selection. This tells the stateful shell to activate an existing branch. A direct location change may be appropriate for deep links, but it should not replace the normal tab-selection behavior.
Sharing navigator keys
Each branch needs its own navigator identity. Reusing one GlobalKey<NavigatorState> for multiple branch navigators can cause assertions or unexpected stack behavior. If you provide branch keys, make every key unique and stable.
Confusing route state with application data
The shell can preserve a route and its widgets, but it is not a data store. If a user edits a profile, save the profile through an application or persistence layer. Do not rely on the branch remaining in memory.
Exercise
The Home tab shows a detail page. After switching to Settings and back, the detail page is gone. Name two likely causes.
Show answer
The app may be recreating the GoRouter, or a custom navigator container may be removing inactive branch children instead of keeping them mounted.