[go_router] Fix popRoute crash when a shell route's navigator is not mounted#12111
[go_router] Fix popRoute crash when a shell route's navigator is not mounted#12111brotoo25 wants to merge 3 commits into
Conversation
…mounted GoRouterDelegate._findCurrentNavigators force-unwraps each shell navigator's state while walking the current configuration. When the system back button is pressed while the configuration references a shell route whose navigator is not mounted (e.g. mid-redirect away from the shell, or while resuming from background), popRoute throws 'Null check operator used on a null value'. Stop walking when a shell navigator is not mounted, matching the null-tolerant handling canPop already uses for the same state. Fixes flutter/flutter#188993
There was a problem hiding this comment.
Code Review
This pull request addresses a potential crash in popRoute() within GoRouterDelegate when a shell route's navigator is not mounted. The implementation replaces a force-unwrap operator with a null check, breaking the traversal loop if the navigator state is null. A widget test simulating this scenario has been added to prevent regressions, and a pending changelog entry has been created. There are no review comments, and I have no feedback to provide.
| while (walker is ShellRouteMatch) { | ||
| final NavigatorState potentialCandidate = walker.navigatorKey.currentState!; | ||
| final NavigatorState? potentialCandidate = walker.navigatorKey.currentState; | ||
| if (potentialCandidate == null) { |
There was a problem hiding this comment.
looking at the issue, do you have a reproducible code that show case the transient state?
also I am not too sure what may be the correct behavior when the backbutton is pressed during that state. It felt wrong to pop the parent navigator because child navigator is in transient state. Perhaps the correct solution will be disallow the current configuration to be in such state
GoRouterDelegate._findCurrentNavigators()force-unwraps each shell navigator's state (walker.navigatorKey.currentState!) while walking the current configuration. When the system back button is pressed whilecurrentConfigurationreferences aShellRoutewhose navigator is not mounted — a transient state during redirects away from a shell or when resuming from background —popRoute()throwsNull check operator used on a null valueand crashes the app (observed in production via crash reporting on go_router 17.3.0, Android).This applies the same null-tolerant handling that
canPop()in the same file already uses for this state: if a shell navigator along the chain is not mounted, stop walking — nothing below it can be mounted either. This mirrors the existing early-breakfor pageless routes in the same loop, and the root navigator in this method is already null-guarded in the same way.The added regression test pins the transient state deterministically (configuration pointing at a shell route while the widget tree shows a non-shell route) and fails on
mainwith the exact production stack trace before this change.Related to, but distinct from, flutter/flutter#187616 and #11911: those cover
currentConfiguration.matchesbeing empty (Bad state: No element); this PR fixes the non-empty case where a shell navigator is unmounted. The two changes are independent and compatible.Fixes flutter/flutter#188993
Pre-Review Checklist
[shared_preferences]///).Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2