Handle didUpdateWidget for _StackNavigator when using a global key #318
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
We found a bug where using a
GlobalKey
for thePageStackNavigator.navigatorKey
causes the internal_StackNavigator
to fail to re-assign itself to the internal page stack. This issue presented itself as the Android back button failing to close non-routemaster routes (like a bottom sheet) and instead just closing the entire app.Inside the
PageStackNavigatorState
, any time thePageStack
changes, it'll invoke a function that recreates the_StackNavigator
widget. Without a key, this would cause the navigator widget to runinitState
, thus setting itself on thePageStack
. However, with a key, the state object is re-used, soinitState
(anddispose
) are never called.This is problematic because if you provide a navigator key for hooking into the navigator throughout your app, the
PageStack
can change but the_StackNavigator
's state functions are never called and thuspop()
calls for non-routemaster routes will ultimately returnfalse
, causing the app to close.Solution
To solve this, all we need to do is override
_StackNavigatorState.didUpdateWidget
and handle the scenario of the internal stack changing -- if it changes, then drop the old navigator reference and add a new navigator reference to the new page stack.FYI @tomgilder