Skip to content

Commit c4ee37c

Browse files
authored
[go_router] Preserve nested pushes during config updates (#12611)
`RouteConfiguration.reparse` currently restores only imperative matches at the root of the match list. Imperative matches created by pushing a route owned by a `StatefulShellRoute` branch are nested inside `ShellRouteMatch.matches`, so a dynamic `RoutingConfig` update silently drops them. This change recursively enumerates imperative matches within shell matches and replays them against the new routing configuration. Rebuilding each match preserves the pushed navigation stack without retaining stale route objects from the previous configuration. The regression test covers this sequence using the go_router test suite's standard A/B route naming: 1. Start at `/a`. 2. Push `/b/details` into another shell branch. 3. Push `/top` above the shell. 4. Update the routing configuration. 5. Pop `/top` and verify that `Screen B Detail` is restored. Fixes flutter/flutter#167344. ## Pre-Review Checklist This is a draft pending the author's review of the contribution, AI-assistance, Tree Hygiene, and CLA requirements above. ### Local verification - `flutter_plugin_tools format --packages go_router` - `flutter_plugin_tools analyze --packages go_router` - `flutter_plugin_tools test --packages go_router` - `flutter_plugin_tools validate --check-for-missing-changes --packages go_router` [^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.
1 parent 2abaedd commit c4ee37c

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,18 @@ class RouteConfiguration {
340340
RouteMatchList reparse(RouteMatchList matchList) {
341341
RouteMatchList result = findMatch(matchList.uri, extra: matchList.extra);
342342

343-
for (final ImperativeRouteMatch imperativeMatch
344-
in matchList.matches.whereType<ImperativeRouteMatch>()) {
345-
final match = ImperativeRouteMatch(
346-
pageKey: imperativeMatch.pageKey,
347-
matches: findMatch(imperativeMatch.matches.uri, extra: imperativeMatch.matches.extra),
348-
completer: imperativeMatch.completer,
349-
);
350-
result = result.push(match);
351-
}
343+
matchList.visitRouteMatches((RouteMatchBase match) {
344+
if (match is ImperativeRouteMatch) {
345+
result = result.push(
346+
ImperativeRouteMatch(
347+
pageKey: match.pageKey,
348+
matches: findMatch(match.matches.uri, extra: match.matches.extra),
349+
completer: match.completer,
350+
),
351+
);
352+
}
353+
return true;
354+
});
352355
return result;
353356
}
354357

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
changelog: Fixes pushed routes nested within a shell being lost when a dynamic routing configuration changes.
2+
version: patch

‎packages/go_router/test/routing_config_test.dart‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:cupertino_ui/cupertino_ui.dart';
5+
import 'dart:async';
6+
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:go_router/go_router.dart';
89
import 'package:material_ui/material_ui.dart';
@@ -136,6 +137,64 @@ void main() {
136137
expect(key.currentState!.value == 1, isTrue);
137138
});
138139

140+
testWidgets('routing config preserves pushed shell routes', (WidgetTester tester) async {
141+
final branchANavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'branch-a');
142+
final branchBNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'branch-b');
143+
RoutingConfig createConfig({required List<RouteBase> additionalRoutes}) {
144+
return RoutingConfig(
145+
routes: <RouteBase>[
146+
GoRoute(path: '/top', builder: (_, _) => const Text('Top-level route')),
147+
StatefulShellRoute.indexedStack(
148+
branches: <StatefulShellBranch>[
149+
StatefulShellBranch(
150+
navigatorKey: branchANavigatorKey,
151+
routes: <RouteBase>[
152+
GoRoute(path: '/a', builder: (_, _) => const Text('Screen A')),
153+
...additionalRoutes,
154+
],
155+
),
156+
StatefulShellBranch(
157+
navigatorKey: branchBNavigatorKey,
158+
routes: <RouteBase>[
159+
GoRoute(
160+
path: '/b',
161+
builder: (_, _) => const Text('Screen B'),
162+
routes: <RouteBase>[
163+
GoRoute(path: 'details', builder: (_, _) => const Text('Screen B Detail')),
164+
],
165+
),
166+
],
167+
),
168+
],
169+
pageBuilder: (_, _, StatefulNavigationShell navigationShell) =>
170+
MaterialPage<void>(child: navigationShell),
171+
),
172+
],
173+
);
174+
}
175+
176+
final config = ValueNotifier<RoutingConfig>(createConfig(additionalRoutes: <RouteBase>[]));
177+
addTearDown(config.dispose);
178+
final GoRouter router = await createRouterWithRoutingConfig(
179+
config,
180+
tester,
181+
initialLocation: '/a',
182+
);
183+
unawaited(router.push('/b/details'));
184+
await tester.pumpAndSettle();
185+
unawaited(router.push('/top'));
186+
await tester.pumpAndSettle();
187+
188+
config.value = createConfig(
189+
additionalRoutes: <RouteBase>[GoRoute(path: '/c', builder: (_, _) => const Text('Screen C'))],
190+
);
191+
await tester.pumpAndSettle();
192+
router.pop();
193+
await tester.pumpAndSettle();
194+
195+
expect(find.text('Screen B Detail'), findsOneWidget);
196+
});
197+
139198
testWidgets(
140199
'routing config works with shell route',
141200
// TODO(tolo): Temporarily skipped due to a bug that causes test to faiL

0 commit comments

Comments
 (0)