Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie
assert(!popped);
return popped;
}
final RouteBase routeBase = match.route;

var leafMatch = match;
while (leafMatch is ShellRouteMatch) {
leafMatch = leafMatch.matches.last;
}

final RouteBase routeBase = leafMatch.route;
if (routeBase is! GoRoute || routeBase.onExit == null) {
route.didPop(result);
_completeRouteMatch(result, match);
Expand All @@ -150,7 +156,7 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie
scheduleMicrotask(() async {
final bool onExitResult = await routeBase.onExit!(
navigatorKey.currentContext!,
match.buildState(_configuration, currentConfiguration),
leafMatch.buildState(_configuration, currentConfiguration),

Copy link
Copy Markdown
Contributor

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

);
if (onExitResult) {
_completeRouteMatch(result, match);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Fixes onExit ignored for GoRoute nested inside ShellRoute
version: patch
78 changes: 78 additions & 0 deletions packages/go_router/test/on_exit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,82 @@ void main() {
expect(find.byKey(detailKey), findsOneWidget);
expect(find.byKey(homeKey), findsNothing);
});

// Regression test for https://github.com/flutter/flutter/issues/137829
testWidgets('back button works synchronously with ShellRoute', (WidgetTester tester) async {
var allow = false;
final home = UniqueKey();
final page = UniqueKey();
final routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, _) => DummyScreen(key: home),
routes: <RouteBase>[
ShellRoute(
builder: (_, _, Widget child) => child,
routes: <RouteBase>[
GoRoute(
path: 'page',
builder: (_, _) => DummyScreen(key: page),
onExit: (BuildContext context, GoRouterState state) => allow,
),
],
),
],
),
];

final GoRouter router = await createRouter(routes, tester, initialLocation: '/page');
expect(find.byKey(page), findsOneWidget);

router.pop();
await tester.pumpAndSettle();
expect(find.byKey(page), findsOneWidget);

allow = true;
router.pop();
await tester.pumpAndSettle();
expect(find.byKey(home), findsOneWidget);
});

// Regression test for https://github.com/flutter/flutter/issues/137829
testWidgets('back button works asynchronously with ShellRoute', (WidgetTester tester) async {
var allow = Completer<bool>();
final home = UniqueKey();
final page = UniqueKey();
final routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, _) => DummyScreen(key: home),
routes: <RouteBase>[
ShellRoute(
builder: (_, _, Widget child) => child,
routes: <RouteBase>[
GoRoute(
path: 'page',
builder: (_, _) => DummyScreen(key: page),
onExit: (BuildContext context, GoRouterState state) async => allow.future,
),
],
),
],
),
];

final GoRouter router = await createRouter(routes, tester, initialLocation: '/page');
expect(find.byKey(page), findsOneWidget);

router.pop();
await tester.pumpAndSettle();
allow.complete(false);
await tester.pumpAndSettle();
expect(find.byKey(page), findsOneWidget);

allow = Completer<bool>();
router.pop();
await tester.pumpAndSettle();
allow.complete(true);
await tester.pumpAndSettle();
expect(find.byKey(home), findsOneWidget);
});
}