Skip to content

Commit

Permalink
[go_router] Fixes replace and pushReplacement uri when only one route… (
Browse files Browse the repository at this point in the history
flutter#7433)

� match in current route match list

fixes flutter#149951
  • Loading branch information
chunhtai authored Aug 20, 2024
1 parent a655a3e commit 1ab1a71
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
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

0 comments on commit 1ab1a71

Please sign in to comment.