Skip to content

Commit 64ec450

Browse files
committed
[go_router] Cover reported shell route regressions
1 parent e4d9867 commit 64ec450

4 files changed

Lines changed: 172 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ class _ShellRoutePageKey extends ValueKey<String> {
342342
_imperativePageKey = imperativePageKey,
343343
super(
344344
imperativePageKey == null
345-
? identityHashCode(route.pageIdentity).toString()
346-
: '${identityHashCode(route.pageIdentity)}-${imperativePageKey.value}',
345+
? route.pageIdentity.hashCode.toString()
346+
: '${route.pageIdentity.hashCode}-${imperativePageKey.value}',
347347
);
348348

349349
final Object _routeIdentity;
@@ -352,12 +352,12 @@ class _ShellRoutePageKey extends ValueKey<String> {
352352
@override
353353
bool operator ==(Object other) {
354354
return other is _ShellRoutePageKey &&
355-
identical(other._routeIdentity, _routeIdentity) &&
355+
other._routeIdentity == _routeIdentity &&
356356
other._imperativePageKey == _imperativePageKey;
357357
}
358358

359359
@override
360-
int get hashCode => Object.hash(identityHashCode(_routeIdentity), _imperativePageKey);
360+
int get hashCode => Object.hash(_routeIdentity.hashCode, _imperativePageKey);
361361
}
362362

363363
class _ShellRouteNavigatorKey extends GlobalKey<NavigatorState> {
@@ -371,12 +371,12 @@ class _ShellRouteNavigatorKey extends GlobalKey<NavigatorState> {
371371
@override
372372
bool operator ==(Object other) {
373373
return other is _ShellRouteNavigatorKey &&
374-
identical(other._routeIdentity, _routeIdentity) &&
374+
other._routeIdentity == _routeIdentity &&
375375
other.imperativePageKey == imperativePageKey;
376376
}
377377

378378
@override
379-
int get hashCode => Object.hash(identityHashCode(_routeIdentity), imperativePageKey);
379+
int get hashCode => Object.hash(_routeIdentity.hashCode, imperativePageKey);
380380
}
381381

382382
/// An matched result by matching a [ShellRoute] against a location.

‎packages/go_router/test/builder_test.dart‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,58 @@ void main() {
9595
expect(find.byType(_DetailsScreen), findsOneWidget);
9696
});
9797

98+
// Regression test for https://github.com/flutter/flutter/issues/148712.
99+
testWidgets('rebuilding GoRouter preserves ShellRoute navigator key', (
100+
WidgetTester tester,
101+
) async {
102+
final rootNavigatorKey = GlobalKey<NavigatorState>();
103+
final shellNavigatorKey = GlobalKey<NavigatorState>();
104+
final routers = <GoRouter>[];
105+
late StateSetter rebuildRouter;
106+
addTearDown(() {
107+
for (final router in routers) {
108+
router.dispose();
109+
}
110+
});
111+
112+
await tester.pumpWidget(
113+
StatefulBuilder(
114+
builder: (BuildContext context, StateSetter setState) {
115+
rebuildRouter = setState;
116+
final router = GoRouter(
117+
navigatorKey: rootNavigatorKey,
118+
routes: <RouteBase>[
119+
ShellRoute(
120+
navigatorKey: shellNavigatorKey,
121+
builder: (_, _, Widget child) => child,
122+
routes: <RouteBase>[
123+
GoRoute(
124+
path: '/',
125+
pageBuilder: (_, GoRouterState state) {
126+
return NoTransitionPage<void>(key: state.pageKey, child: const SizedBox());
127+
},
128+
),
129+
],
130+
),
131+
],
132+
);
133+
routers.add(router);
134+
return MaterialApp.router(routerConfig: router);
135+
},
136+
),
137+
);
138+
await tester.pumpAndSettle();
139+
expect(tester.takeException(), isNull);
140+
final NavigatorState shellNavigator = shellNavigatorKey.currentState!;
141+
142+
rebuildRouter(() {});
143+
await tester.pumpAndSettle();
144+
145+
expect(tester.takeException(), isNull);
146+
expect(routers, hasLength(2));
147+
expect(shellNavigatorKey.currentState, same(shellNavigator));
148+
});
149+
98150
testWidgets('Uses the correct navigatorKey', (WidgetTester tester) async {
99151
final rootNavigatorKey = GlobalKey<NavigatorState>();
100152
final RouteConfiguration config = createRouteConfiguration(

‎packages/go_router/test/imperative_api_test.dart‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,50 @@ void main() {
310310
expect(find.byKey(secondPageKey), findsOneWidget);
311311
});
312312

313+
testWidgets('push sequence from flutter/flutter#140586 does not duplicate page keys', (
314+
WidgetTester tester,
315+
) async {
316+
NoTransitionPage<void> page(GoRouterState state, String label) {
317+
return NoTransitionPage<void>(key: state.pageKey, child: Text(label));
318+
}
319+
320+
final routes = <RouteBase>[
321+
GoRoute(path: '/a', pageBuilder: (_, GoRouterState state) => page(state, 'A Screen')),
322+
ShellRoute(
323+
navigatorKey: GlobalKey<NavigatorState>(),
324+
pageBuilder: (_, GoRouterState state, Widget child) {
325+
return NoTransitionPage<void>(key: state.pageKey, child: child);
326+
},
327+
routes: <RouteBase>[
328+
GoRoute(path: '/b', pageBuilder: (_, GoRouterState state) => page(state, 'B Screen')),
329+
GoRoute(path: '/c', pageBuilder: (_, GoRouterState state) => page(state, 'C Screen')),
330+
],
331+
),
332+
];
333+
final GoRouter router = await createRouter(routes, tester, initialLocation: '/a');
334+
expect(find.text('A Screen'), findsOneWidget);
335+
336+
router.push<void>('/b');
337+
await tester.pumpAndSettle();
338+
expect(tester.takeException(), isNull);
339+
expect(find.text('B Screen'), findsOneWidget);
340+
341+
router.push<void>('/c');
342+
await tester.pumpAndSettle();
343+
expect(tester.takeException(), isNull);
344+
expect(find.text('C Screen'), findsOneWidget);
345+
346+
router.pushReplacement<void>('/a');
347+
await tester.pumpAndSettle();
348+
expect(tester.takeException(), isNull);
349+
expect(find.text('A Screen'), findsOneWidget);
350+
351+
router.push<void>('/b');
352+
await tester.pumpAndSettle();
353+
expect(tester.takeException(), isNull);
354+
expect(find.text('B Screen'), findsOneWidget);
355+
});
356+
313357
testWidgets('push inside or outside shell route', (WidgetTester tester) async {
314358
// Regression test for https://github.com/flutter/flutter/issues/120665.
315359
final inside = UniqueKey();

‎packages/go_router/test/match_test.dart‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ void main() {
204204

205205
test('push scopes key collisions in nested ShellRouteMatch', () {
206206
final leafRoute = GoRoute(path: '/leaf', builder: _builder);
207-
final nestedNavigatorKey = GlobalKey<NavigatorState>();
207+
// Deliberately create separate instances to verify value equality below.
208+
// ignore: prefer_const_constructors
209+
final nestedNavigatorKey = _ValueNavigatorKey('nested');
208210
final nestedRoute = ShellRoute(
209211
navigatorKey: nestedNavigatorKey,
210212
builder: _shellBuilder,
@@ -276,6 +278,61 @@ void main() {
276278
expect(pushedOuterMatch.navigatorKey, same(pushedOuterRoute.navigatorKey));
277279
expect(pushedNestedMatch.pageKey, isNot(nestedPageKey));
278280
expect(pushedNestedMatch.navigatorKey, isNot(equals(nestedNavigatorKey)));
281+
282+
// ignore: prefer_const_constructors
283+
final equivalentNestedNavigatorKey = _ValueNavigatorKey('nested');
284+
expect(equivalentNestedNavigatorKey, equals(nestedNavigatorKey));
285+
expect(equivalentNestedNavigatorKey, isNot(same(nestedNavigatorKey)));
286+
final equivalentLeafRoute = GoRoute(path: '/leaf', builder: _builder);
287+
final equivalentNestedRoute = ShellRoute(
288+
navigatorKey: equivalentNestedNavigatorKey,
289+
builder: _shellBuilder,
290+
routes: <RouteBase>[equivalentLeafRoute],
291+
);
292+
final equivalentPushedOuterRoute = ShellRoute(
293+
builder: _shellBuilder,
294+
routes: <RouteBase>[equivalentNestedRoute],
295+
);
296+
final equivalentPushedMatchList = RouteMatchList(
297+
matches: <RouteMatchBase>[
298+
ShellRouteMatch(
299+
route: equivalentPushedOuterRoute,
300+
matches: <RouteMatchBase>[
301+
ShellRouteMatch(
302+
route: equivalentNestedRoute,
303+
matches: <RouteMatchBase>[
304+
RouteMatch(
305+
route: equivalentLeafRoute,
306+
matchedLocation: '/leaf',
307+
pageKey: const ValueKey<String>('/leaf'),
308+
),
309+
],
310+
matchedLocation: '/leaf',
311+
pageKey: nestedPageKey,
312+
navigatorKey: equivalentNestedNavigatorKey,
313+
),
314+
],
315+
matchedLocation: '/leaf',
316+
pageKey: pushedOuterPageKey,
317+
navigatorKey: equivalentPushedOuterRoute.navigatorKey,
318+
),
319+
],
320+
uri: Uri.parse('/leaf'),
321+
pathParameters: const <String, String>{},
322+
);
323+
final RouteMatchList equivalentResult = currentMatchList.push(
324+
ImperativeRouteMatch(
325+
pageKey: const ValueKey<String>('push'),
326+
matches: equivalentPushedMatchList,
327+
completer: Completer<void>(),
328+
),
329+
);
330+
final equivalentPushedOuterMatch = equivalentResult.matches.last as ShellRouteMatch;
331+
final equivalentPushedNestedMatch =
332+
equivalentPushedOuterMatch.matches.single as ShellRouteMatch;
333+
expect(equivalentPushedNestedMatch.pageKey, pushedNestedMatch.pageKey);
334+
expect(equivalentPushedNestedMatch.pageKey.value, pushedNestedMatch.pageKey.value);
335+
expect(equivalentPushedNestedMatch.navigatorKey, pushedNestedMatch.navigatorKey);
279336
});
280337
});
281338
}
@@ -284,3 +341,15 @@ Widget _builder(BuildContext context, GoRouterState state) => const Placeholder(
284341

285342
Widget _shellBuilder(BuildContext context, GoRouterState state, Widget child) =>
286343
const Placeholder();
344+
345+
class _ValueNavigatorKey extends GlobalKey<NavigatorState> {
346+
const _ValueNavigatorKey(this.label) : super.constructor();
347+
348+
final String label;
349+
350+
@override
351+
bool operator ==(Object other) => other is _ValueNavigatorKey && other.label == label;
352+
353+
@override
354+
int get hashCode => label.hashCode;
355+
}

0 commit comments

Comments
 (0)