Skip to content

[go_router] fix onExit ignored for GoRoute nested inside ShellRoute#11853

Open
qiqetes wants to merge 2 commits into
flutter:mainfrom
qiqetes:fix/onExit-inside-shellRoute
Open

[go_router] fix onExit ignored for GoRoute nested inside ShellRoute#11853
qiqetes wants to merge 2 commits into
flutter:mainfrom
qiqetes:fix/onExit-inside-shellRoute

Conversation

@qiqetes
Copy link
Copy Markdown

@qiqetes qiqetes commented Jun 6, 2026

onExit was silently ignored when the route was nested inside a ShellRoute + added test for async and sync pop for this specific case
Attempt 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

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-assist bot 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

  1. 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

  ShellRoute + added test for async and sync pop for this specific case
@github-actions github-actions Bot added p: go_router triage-framework Should be looked at in framework triage labels Jun 6, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

Thanks for the contribution!

[ ] I followed the version and CHANGELOG instructions, using semantic versioning and the repository CHANGELOG style, or I have commented below to indicate which documented exception this PR falls under1.

Once you've completed the checklist, please mark the PR as ready for review so that it can be assigned a reviewer.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft June 6, 2026 22:12
@qiqetes qiqetes marked this pull request as ready for review June 6, 2026 23:13
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);
      }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: go_router triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GoRouter] onExit does not work with ShellRoute

2 participants