[go_router] fix onExit ignored for GoRoute nested inside ShellRoute#11853
[go_router] fix onExit ignored for GoRoute nested inside ShellRoute#11853qiqetes wants to merge 2 commits into
Conversation
ShellRoute + added test for async and sync pop for this specific case
There was a problem hiding this comment.
Code Review
This pull request updates GoRouterDelegate to resolve the leaf match when handling a ShellRouteMatch by traversing nested matches to find the final leaf route. This ensures that the correct route and state are used when invoking the onExit callback. Additionally, synchronous and asynchronous regression tests have been added to verify back button behavior with ShellRoute. There are no review comments, and I have no feedback to provide.
|
Thanks for the contribution!
Once you've completed the checklist, please mark the PR as ready for review so that it can be assigned a reviewer. |
There was a problem hiding this comment.
Code Review
This pull request updates GoRouterDelegate to correctly handle onExit callbacks for GoRoutes nested inside a ShellRoute by traversing to the leaf match, and adds corresponding regression tests. A review comment identifies a potential null pointer exception when force-unwrapping navigatorKey.currentContext inside scheduleMicrotask, as the context could be null if the widget tree is unmounted before the microtask executes, and suggests adding a defensive null check.
| final bool onExitResult = await routeBase.onExit!( | ||
| navigatorKey.currentContext!, | ||
| match.buildState(_configuration, currentConfiguration), | ||
| leafMatch.buildState(_configuration, currentConfiguration), |
There was a problem hiding this comment.
Since onExit is executed asynchronously inside a scheduleMicrotask, there is a possibility that the widget tree has been unmounted or the navigator disposed by the time the microtask runs, making navigatorKey.currentContext null. Calling navigatorKey.currentContext! will then trigger a null check operator error.
Consider defensively checking if the context is null before proceeding, and if so, fallback to completing the route match:
scheduleMicrotask(() async {
final BuildContext? context = navigatorKey.currentContext;
if (context == null) {
_completeRouteMatch(result, match);
return;
}
final bool onExitResult = await routeBase.onExit!(
context,
leafMatch.buildState(_configuration, currentConfiguration),
);
if (onExitResult) {
_completeRouteMatch(result, match);
}
});
onExitwas silently ignored when the route was nested inside a ShellRoute + added test for async and sync pop for this specific caseAttempt to fix flutter/flutter#137829 flutter/flutter#137829
List which issues are fixed by this PR. You must list at least one issue.
Pre-Review Checklist
[shared_preferences]///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.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