Skip to content

Commit cb01eda

Browse files
authored
[go_router] Fixes RouteInformationParser that does not restore full RouteMatchList if the optionURLReflectsImperativeAPIs is set (flutter#4713)
fixes flutter#131457
1 parent 98659b7 commit cb01eda

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 10.1.4
2+
3+
- Fixes RouteInformationParser that does not restore full RouteMatchList if
4+
the optionURLReflectsImperativeAPIs is set.
5+
16
## 10.1.3
27

38
- Fixes an issue in the documentation that was using `state.queryParameters` instead of `state.uri.queryParameters`.

packages/go_router/lib/src/parser.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,21 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
129129
if (configuration.isEmpty) {
130130
return null;
131131
}
132+
final String location;
132133
if (GoRouter.optionURLReflectsImperativeAPIs &&
133134
configuration.matches.last is ImperativeRouteMatch) {
134-
configuration =
135-
(configuration.matches.last as ImperativeRouteMatch).matches;
135+
location = (configuration.matches.last as ImperativeRouteMatch)
136+
.matches
137+
.uri
138+
.toString();
139+
} else {
140+
location = configuration.uri.toString();
136141
}
137142
return RouteInformation(
138143
// TODO(chunhtai): remove this ignore and migrate the code
139144
// https://github.com/flutter/flutter/issues/124045.
140145
// ignore: deprecated_member_use
141-
location: configuration.uri.toString(),
146+
location: location,
142147
state: _routeMatchListCodec.encode(configuration),
143148
);
144149
}

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: 10.1.3
4+
version: 10.1.4
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/go_router_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,11 +1046,10 @@ void main() {
10461046
final RouteMatchListCodec codec =
10471047
RouteMatchListCodec(router.configuration);
10481048
await tester.pumpAndSettle();
1049-
final ImperativeRouteMatch match = router
1050-
.routerDelegate.currentConfiguration.last as ImperativeRouteMatch;
10511049
expect(log, <Object>[
10521050
isMethodCall('selectMultiEntryHistory', arguments: null),
1053-
IsRouteUpdateCall('/settings', false, codec.encode(match.matches)),
1051+
IsRouteUpdateCall('/settings', false,
1052+
codec.encode(router.routerDelegate.currentConfiguration)),
10541053
]);
10551054
GoRouter.optionURLReflectsImperativeAPIs = false;
10561055
});

packages/go_router/test/parser_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
66
import 'package:flutter_test/flutter_test.dart';
77
import 'package:go_router/go_router.dart';
88

9+
import 'test_helpers.dart';
10+
911
RouteInformation createRouteInformation(String location, [Object? extra]) {
1012
return RouteInformation(
1113
// TODO(chunhtai): remove this ignore and migrate the code
@@ -81,6 +83,53 @@ void main() {
8183
expect(matches[1].route, routes[0].routes[0]);
8284
});
8385

86+
testWidgets(
87+
'GoRouteInformationParser can restore full route matches if optionURLReflectsImperativeAPIs is true',
88+
(WidgetTester tester) async {
89+
final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
90+
final List<GoRoute> routes = <GoRoute>[
91+
GoRoute(
92+
path: '/',
93+
builder: (_, __) => const Placeholder(),
94+
routes: <GoRoute>[
95+
GoRoute(
96+
path: 'abc',
97+
builder: (_, __) => const Placeholder(),
98+
),
99+
],
100+
),
101+
];
102+
GoRouter.optionURLReflectsImperativeAPIs = true;
103+
final GoRouter router =
104+
await createRouter(routes, tester, navigatorKey: navKey);
105+
106+
// Generate RouteMatchList with imperative route match
107+
router.go('/abc');
108+
await tester.pumpAndSettle();
109+
router.push('/');
110+
await tester.pumpAndSettle();
111+
final RouteMatchList matchList = router.routerDelegate.currentConfiguration;
112+
expect(matchList.uri.toString(), '/abc');
113+
expect(matchList.matches.length, 3);
114+
115+
final RouteInformation restoredRouteInformation =
116+
router.routeInformationParser.restoreRouteInformation(matchList)!;
117+
// URL reflects the latest push.
118+
// TODO(chunhtai): remove this ignore and migrate the code
119+
// https://github.com/flutter/flutter/issues/124045.
120+
// ignore: deprecated_member_use
121+
expect(restoredRouteInformation.location, '/');
122+
123+
// Can restore back to original RouteMatchList.
124+
final RouteMatchList parsedRouteMatch = await router.routeInformationParser
125+
.parseRouteInformationWithDependencies(
126+
restoredRouteInformation, navKey.currentContext!);
127+
expect(parsedRouteMatch.uri.toString(), '/abc');
128+
expect(parsedRouteMatch.matches.length, 3);
129+
130+
GoRouter.optionURLReflectsImperativeAPIs = false;
131+
});
132+
84133
test('GoRouteInformationParser can retrieve route by name', () async {
85134
final List<GoRoute> routes = <GoRoute>[
86135
GoRoute(

0 commit comments

Comments
 (0)