Skip to content

Commit c36eb12

Browse files
committed
[go_router] Fix duplicate page key when re-entering a shell route
When a route inside a `ShellRoute` is imperatively pushed while a previous instance of that same shell route is still in the navigation stack (for example pushing into a shell after a `pushReplacement`), go_router cloned the shell route match while reusing the shell's page key and navigator key. The two shell pages and navigators then shared the same keys, tripping the framework's `!keyReservation.contains(key)` duplicate page key assertion (and, in turn, a duplicate `GlobalKey`). The cloned shell instance now receives distinct page and navigator keys derived from the imperative match's stable page key, so they remain consistent across reparses. Shell routes reached via `go` or appearing only once keep their natural route-derived keys, leaving `parentNavigatorKey` and explicit `navigatorKey` behavior unchanged. Fixes flutter/flutter#140586
1 parent 6246800 commit c36eb12

4 files changed

Lines changed: 163 additions & 10 deletions

File tree

‎packages/go_router/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 17.2.4
2+
3+
- Fixes a duplicate page key assertion when imperatively re-entering a shell route that is still in the navigation stack (e.g. pushing into a shell after a `pushReplacement`).
4+
15
## 17.2.3
26

37
- Fixes an assertion failure when navigating to URLs with hash fragments missing a leading slash.

‎packages/go_router/lib/src/match.dart‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,17 @@ class ShellRouteMatch extends RouteMatchBase {
424424
// TODO(loic-sharma): Remove meta library prefix.
425425
// https://github.com/flutter/flutter/issues/171410
426426
@meta.internal
427-
ShellRouteMatch copyWith({required List<RouteMatchBase>? matches}) {
427+
ShellRouteMatch copyWith({
428+
List<RouteMatchBase>? matches,
429+
ValueKey<String>? pageKey,
430+
GlobalKey<NavigatorState>? navigatorKey,
431+
}) {
428432
return ShellRouteMatch(
429433
matches: matches ?? this.matches,
430434
route: route,
431435
matchedLocation: matchedLocation,
432-
pageKey: pageKey,
433-
navigatorKey: navigatorKey,
436+
pageKey: pageKey ?? this.pageKey,
437+
navigatorKey: navigatorKey ?? this.navigatorKey,
434438
);
435439
}
436440

@@ -654,22 +658,67 @@ class RouteMatchList with Diagnosticable {
654658
);
655659
return newMatches;
656660
}
661+
// Collect the page keys already in use so that re-entering a shell route
662+
// that is still in the stack does not reuse its page and navigator keys.
663+
final usedPageKeys = <String>{};
664+
_collectPageKeys(newMatches, usedPageKeys);
657665
newMatches.add(
658-
_cloneBranchAndInsertImperativeMatch(otherMatches.last, match),
666+
_cloneBranchAndInsertImperativeMatch(
667+
otherMatches.last,
668+
match,
669+
usedPageKeys,
670+
),
659671
);
660672
return newMatches;
661673
}
662674

675+
/// Recursively collects the [RouteMatchBase.pageKey] values of `matches`.
676+
static void _collectPageKeys(
677+
List<RouteMatchBase> matches,
678+
Set<String> result,
679+
) {
680+
for (final match in matches) {
681+
result.add(match.pageKey.value);
682+
if (match is ShellRouteMatch) {
683+
_collectPageKeys(match.matches, result);
684+
}
685+
}
686+
}
687+
663688
static RouteMatchBase _cloneBranchAndInsertImperativeMatch(
664689
RouteMatchBase branch,
665690
ImperativeRouteMatch match,
691+
Set<String> usedPageKeys,
666692
) {
667693
if (branch is ShellRouteMatch) {
668-
return branch.copyWith(
669-
matches: <RouteMatchBase>[
670-
_cloneBranchAndInsertImperativeMatch(branch.matches.last, match),
671-
],
672-
);
694+
final matches = <RouteMatchBase>[
695+
_cloneBranchAndInsertImperativeMatch(
696+
branch.matches.last,
697+
match,
698+
usedPageKeys,
699+
),
700+
];
701+
// A [ShellRouteMatch] derives its page key and navigator key from its
702+
// route, so re-entering the same shell route while a previous instance is
703+
// still in the stack would produce two pages and two navigators sharing
704+
// the same keys. That trips the framework's duplicate page key assertion
705+
// (and, in turn, a duplicate [GlobalKey]). Give the new instance distinct
706+
// keys derived from the imperative match's (stable) page key so they stay
707+
// consistent across reparses.
708+
// Regression test for https://github.com/flutter/flutter/issues/140586.
709+
if (usedPageKeys.contains(branch.pageKey.value)) {
710+
final pageKey = ValueKey<String>(
711+
'${match.pageKey.value}${branch.pageKey.value}',
712+
);
713+
usedPageKeys.add(pageKey.value);
714+
return branch.copyWith(
715+
matches: matches,
716+
pageKey: pageKey,
717+
navigatorKey: GlobalObjectKey<NavigatorState>(match.pageKey),
718+
);
719+
}
720+
usedPageKeys.add(branch.pageKey.value);
721+
return branch.copyWith(matches: matches);
673722
}
674723
// Add the input `match` instead of the incompatibleMatch since it contains
675724
// page key and push future.

‎packages/go_router/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 17.2.3
4+
version: 17.2.4
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

‎packages/go_router/test/imperative_api_test.dart‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,104 @@ void main() {
316316
expect(find.text('shell'), findsNothing);
317317
expect(find.byKey(e), findsOneWidget);
318318
});
319+
320+
testWidgets('re-enter a shell route still in the stack', (
321+
WidgetTester tester,
322+
) async {
323+
// Regression test for https://github.com/flutter/flutter/issues/140586.
324+
final shellKey = GlobalKey<NavigatorState>();
325+
final a = UniqueKey();
326+
final b = UniqueKey();
327+
final c = UniqueKey();
328+
final routes = <RouteBase>[
329+
GoRoute(
330+
path: '/a',
331+
builder: (_, __) => DummyScreen(key: a),
332+
),
333+
ShellRoute(
334+
navigatorKey: shellKey,
335+
builder: (_, __, Widget child) => child,
336+
routes: <RouteBase>[
337+
GoRoute(
338+
path: '/b',
339+
builder: (_, __) => DummyScreen(key: b),
340+
),
341+
GoRoute(
342+
path: '/c',
343+
builder: (_, __) => DummyScreen(key: c),
344+
),
345+
],
346+
),
347+
];
348+
final GoRouter router = await createRouter(
349+
routes,
350+
tester,
351+
initialLocation: '/a',
352+
);
353+
354+
router.push('/b');
355+
await tester.pumpAndSettle();
356+
router.push('/c');
357+
await tester.pumpAndSettle();
358+
router.pushReplacement('/a');
359+
await tester.pumpAndSettle();
360+
361+
// Re-entering the shell route while a previous instance is still in the
362+
// stack used to throw a duplicate page key assertion.
363+
router.push('/b');
364+
await tester.pumpAndSettle();
365+
expect(tester.takeException(), isNull);
366+
expect(find.byKey(b), findsOneWidget);
367+
368+
// Popping the re-entered shell restores the replaced top-level page.
369+
router.pop();
370+
await tester.pumpAndSettle();
371+
expect(tester.takeException(), isNull);
372+
expect(find.byKey(a), findsOneWidget);
373+
});
374+
375+
testWidgets('re-enter a nested shell route still in the stack', (
376+
WidgetTester tester,
377+
) async {
378+
// Regression test for https://github.com/flutter/flutter/issues/140586.
379+
// Variant with a nested shell and auto-generated navigator keys.
380+
final a = UniqueKey();
381+
final b = UniqueKey();
382+
final c = UniqueKey();
383+
final routes = <RouteBase>[
384+
GoRoute(
385+
path: '/a',
386+
builder: (_, __) => DummyScreen(key: a),
387+
routes: <RouteBase>[
388+
ShellRoute(
389+
builder: (_, __, Widget child) => child,
390+
routes: <RouteBase>[
391+
GoRoute(
392+
path: 'b',
393+
builder: (_, __) => DummyScreen(key: b),
394+
),
395+
],
396+
),
397+
],
398+
),
399+
GoRoute(
400+
path: '/c',
401+
builder: (_, __) => DummyScreen(key: c),
402+
),
403+
];
404+
final GoRouter router = await createRouter(
405+
routes,
406+
tester,
407+
initialLocation: '/a',
408+
);
409+
410+
router.push('/a/b');
411+
await tester.pumpAndSettle();
412+
router.push('/c');
413+
await tester.pumpAndSettle();
414+
router.push('/a/b');
415+
await tester.pumpAndSettle();
416+
expect(tester.takeException(), isNull);
417+
expect(find.byKey(b), findsOneWidget);
418+
});
319419
}

0 commit comments

Comments
 (0)