Keep navigation state predictable
Navigation can go out of sync when imperative calls and list updates both change the same stack.
Keep stack updates in the page list
Use List<PageEntry> to update the stack managed by state. Mixing in Navigator.push or Navigator.pop can put the stack out of sync.
Build widgets from page state
Keep page IDs and metadata in state. Create the widgets when you resolve a page.
Use a unique key for each page
Give each page in a stack a unique PageEntry.key. Reusing a key in that stack can rebuild a page unexpectedly.
Check a migration before merging it
Use the checklist in reviews to catch changes that would put the state and visible pages out of sync.
Two ways to update a stack
// Bad: mixing imperative and declarative for same stack
Navigator.of(context).push(...);
_pages = [..._pages, const PageEntry(key: 'detail', name: '/detail')];
// Good: update only declarative state
setState(() {
_pages = [..._pages, const PageEntry(key: 'detail', name: '/detail')];
});
Trace where the stack changes
In a code review, check that one place owns each stack before approving navigation changes.
Previous
Handle BackNext
Overview