Skip to content

[go_router] Fixes replace and pushReplacement uri when only one route… #7433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2024
Merged
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
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 14.2.6

- Fixes replace and pushReplacement uri when only one route match in current route match list.

## 14.2.5

- Fixes an issue where android back button pops pages in the wrong order.
Expand Down
37 changes: 22 additions & 15 deletions packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
location = safeRoute.matches.uri.toString();
}
}

return RouteInformation(
uri: Uri.parse(location ?? configuration.uri.toString()),
state: _routeMatchListCodec.encode(configuration),
Expand Down Expand Up @@ -194,22 +193,30 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
);
case NavigatingType.pushReplacement:
final RouteMatch routeMatch = baseRouteMatchList!.last;
return baseRouteMatchList.remove(routeMatch).push(
ImperativeRouteMatch(
pageKey: _getUniqueValueKey(),
completer: completer!,
matches: newMatchList,
),
);
baseRouteMatchList = baseRouteMatchList.remove(routeMatch);
if (baseRouteMatchList.isEmpty) {
return newMatchList;
}
return baseRouteMatchList.push(
ImperativeRouteMatch(
pageKey: _getUniqueValueKey(),
completer: completer!,
matches: newMatchList,
),
);
case NavigatingType.replace:
final RouteMatch routeMatch = baseRouteMatchList!.last;
return baseRouteMatchList.remove(routeMatch).push(
ImperativeRouteMatch(
pageKey: routeMatch.pageKey,
completer: completer!,
matches: newMatchList,
),
);
baseRouteMatchList = baseRouteMatchList.remove(routeMatch);
if (baseRouteMatchList.isEmpty) {
return newMatchList;
}
return baseRouteMatchList.push(
ImperativeRouteMatch(
pageKey: routeMatch.pageKey,
completer: completer!,
matches: newMatchList,
),
);
case NavigatingType.go:
return newMatchList;
case NavigatingType.restore:
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 14.2.5
version: 14.2.6
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
4 changes: 1 addition & 3 deletions packages/go_router/test/extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ void main() {
final GoRouter router = await createGoRouter(tester);
await tester.tap(find.text('Settings'));
await tester.pumpAndSettle();
final ImperativeRouteMatch routeMatch = router
.routerDelegate.currentConfiguration.last as ImperativeRouteMatch;
expect(routeMatch.matches.uri.toString(),
expect(router.routerDelegate.currentConfiguration.uri.toString(),
'/page-0/settings?search=notification');
});
});
Expand Down
22 changes: 22 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ void main() {
expect(find.byType(DummyScreen), findsOneWidget);
});

testWidgets('pushReplacement and replace when only one matches',
(WidgetTester tester) async {
final List<GoRoute> routes = <GoRoute>[
GoRoute(name: '1', path: '/', builder: dummy),
GoRoute(name: '2', path: '/a', builder: dummy),
GoRoute(name: '3', path: '/b', builder: dummy),
];

final GoRouter router = await createRouter(routes, tester);
expect(router.routerDelegate.currentConfiguration.uri.path, '/');

router.replace<void>('/a');
await tester.pumpAndSettle();
// When the imperative match is the only match in the route match list,
// it should update the uri.
expect(router.routerDelegate.currentConfiguration.uri.path, '/a');

router.pushReplacement<void>('/b');
await tester.pumpAndSettle();
expect(router.routerDelegate.currentConfiguration.uri.path, '/b');
});

test('empty path', () {
expect(() {
GoRoute(path: '');
Expand Down