Skip to content

Commit 1ab1a71

Browse files
authored
[go_router] Fixes replace and pushReplacement uri when only one route… (#7433)
� match in current route match list fixes flutter/flutter#149951
1 parent a655a3e commit 1ab1a71

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 14.2.6
2+
3+
- Fixes replace and pushReplacement uri when only one route match in current route match list.
4+
15
## 14.2.5
26

37
- Fixes an issue where android back button pops pages in the wrong order.

packages/go_router/lib/src/parser.dart

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
160160
location = safeRoute.matches.uri.toString();
161161
}
162162
}
163-
164163
return RouteInformation(
165164
uri: Uri.parse(location ?? configuration.uri.toString()),
166165
state: _routeMatchListCodec.encode(configuration),
@@ -194,22 +193,30 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
194193
);
195194
case NavigatingType.pushReplacement:
196195
final RouteMatch routeMatch = baseRouteMatchList!.last;
197-
return baseRouteMatchList.remove(routeMatch).push(
198-
ImperativeRouteMatch(
199-
pageKey: _getUniqueValueKey(),
200-
completer: completer!,
201-
matches: newMatchList,
202-
),
203-
);
196+
baseRouteMatchList = baseRouteMatchList.remove(routeMatch);
197+
if (baseRouteMatchList.isEmpty) {
198+
return newMatchList;
199+
}
200+
return baseRouteMatchList.push(
201+
ImperativeRouteMatch(
202+
pageKey: _getUniqueValueKey(),
203+
completer: completer!,
204+
matches: newMatchList,
205+
),
206+
);
204207
case NavigatingType.replace:
205208
final RouteMatch routeMatch = baseRouteMatchList!.last;
206-
return baseRouteMatchList.remove(routeMatch).push(
207-
ImperativeRouteMatch(
208-
pageKey: routeMatch.pageKey,
209-
completer: completer!,
210-
matches: newMatchList,
211-
),
212-
);
209+
baseRouteMatchList = baseRouteMatchList.remove(routeMatch);
210+
if (baseRouteMatchList.isEmpty) {
211+
return newMatchList;
212+
}
213+
return baseRouteMatchList.push(
214+
ImperativeRouteMatch(
215+
pageKey: routeMatch.pageKey,
216+
completer: completer!,
217+
matches: newMatchList,
218+
),
219+
);
213220
case NavigatingType.go:
214221
return newMatchList;
215222
case NavigatingType.restore:

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: 14.2.5
4+
version: 14.2.6
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/extension_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ void main() {
3838
final GoRouter router = await createGoRouter(tester);
3939
await tester.tap(find.text('Settings'));
4040
await tester.pumpAndSettle();
41-
final ImperativeRouteMatch routeMatch = router
42-
.routerDelegate.currentConfiguration.last as ImperativeRouteMatch;
43-
expect(routeMatch.matches.uri.toString(),
41+
expect(router.routerDelegate.currentConfiguration.uri.toString(),
4442
'/page-0/settings?search=notification');
4543
});
4644
});

packages/go_router/test/go_router_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ void main() {
6868
expect(find.byType(DummyScreen), findsOneWidget);
6969
});
7070

71+
testWidgets('pushReplacement and replace when only one matches',
72+
(WidgetTester tester) async {
73+
final List<GoRoute> routes = <GoRoute>[
74+
GoRoute(name: '1', path: '/', builder: dummy),
75+
GoRoute(name: '2', path: '/a', builder: dummy),
76+
GoRoute(name: '3', path: '/b', builder: dummy),
77+
];
78+
79+
final GoRouter router = await createRouter(routes, tester);
80+
expect(router.routerDelegate.currentConfiguration.uri.path, '/');
81+
82+
router.replace<void>('/a');
83+
await tester.pumpAndSettle();
84+
// When the imperative match is the only match in the route match list,
85+
// it should update the uri.
86+
expect(router.routerDelegate.currentConfiguration.uri.path, '/a');
87+
88+
router.pushReplacement<void>('/b');
89+
await tester.pumpAndSettle();
90+
expect(router.routerDelegate.currentConfiguration.uri.path, '/b');
91+
});
92+
7193
test('empty path', () {
7294
expect(() {
7395
GoRoute(path: '');

0 commit comments

Comments
 (0)