Skip to content

Commit c7d30e2

Browse files
authored
[go_router]: fix GoRouter.optionURLReflectsImperativeAPIs flag works with new imperative APIs (#6236)
After 13.0.0 release of go_router package `GoRouter.optionURLReflectsImperativeAPIs` is not working correct. Isn't correct = url in browser doesn't updates after push, example you can see in new test, or in linked issue [List which issues are fixed by this PR. You must list at least one issue.](flutter/flutter#142053)
1 parent ead738f commit c7d30e2

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
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+
## 13.2.2
2+
3+
- Fixes restoreRouteInformation issue when GoRouter.optionURLReflectsImperativeAPIs is true and the last match is ShellRouteMatch
4+
15
## 13.2.1
26

37
- Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.

packages/go_router/lib/src/parser.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,27 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
125125
if (configuration.isEmpty) {
126126
return null;
127127
}
128-
final String location;
128+
String? location;
129129
if (GoRouter.optionURLReflectsImperativeAPIs &&
130-
configuration.matches.last is ImperativeRouteMatch) {
131-
location = (configuration.matches.last as ImperativeRouteMatch)
132-
.matches
133-
.uri
134-
.toString();
135-
} else {
136-
location = configuration.uri.toString();
130+
(configuration.matches.last is ImperativeRouteMatch ||
131+
configuration.matches.last is ShellRouteMatch)) {
132+
RouteMatchBase route = configuration.matches.last;
133+
134+
while (route is! ImperativeRouteMatch) {
135+
if (route is ShellRouteMatch && route.matches.isNotEmpty) {
136+
route = route.matches.last;
137+
} else {
138+
break;
139+
}
140+
}
141+
142+
if (route case final ImperativeRouteMatch safeRoute) {
143+
location = safeRoute.matches.uri.toString();
144+
}
137145
}
146+
138147
return RouteInformation(
139-
uri: Uri.parse(location),
148+
uri: Uri.parse(location ?? configuration.uri.toString()),
140149
state: _routeMatchListCodec.encode(configuration),
141150
);
142151
}

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: 13.2.1
4+
version: 13.2.2
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,47 @@ void main() {
11111111
log.clear();
11121112
});
11131113

1114+
testWidgets(
1115+
'on push shell route with optionURLReflectImperativeAPIs = true',
1116+
(WidgetTester tester) async {
1117+
GoRouter.optionURLReflectsImperativeAPIs = true;
1118+
final List<RouteBase> routes = <RouteBase>[
1119+
GoRoute(
1120+
path: '/',
1121+
builder: (BuildContext context, GoRouterState state) =>
1122+
const DummyScreen(),
1123+
routes: <RouteBase>[
1124+
ShellRoute(
1125+
builder:
1126+
(BuildContext context, GoRouterState state, Widget child) =>
1127+
child,
1128+
routes: <RouteBase>[
1129+
GoRoute(
1130+
path: 'c',
1131+
builder: (BuildContext context, GoRouterState state) =>
1132+
const DummyScreen(),
1133+
)
1134+
],
1135+
),
1136+
],
1137+
),
1138+
];
1139+
1140+
final GoRouter router = await createRouter(routes, tester);
1141+
1142+
log.clear();
1143+
router.push('/c?foo=bar');
1144+
final RouteMatchListCodec codec =
1145+
RouteMatchListCodec(router.configuration);
1146+
await tester.pumpAndSettle();
1147+
expect(log, <Object>[
1148+
isMethodCall('selectMultiEntryHistory', arguments: null),
1149+
IsRouteUpdateCall('/c?foo=bar', false,
1150+
codec.encode(router.routerDelegate.currentConfiguration)),
1151+
]);
1152+
GoRouter.optionURLReflectsImperativeAPIs = false;
1153+
});
1154+
11141155
testWidgets('on push with optionURLReflectImperativeAPIs = true',
11151156
(WidgetTester tester) async {
11161157
GoRouter.optionURLReflectsImperativeAPIs = true;

0 commit comments

Comments
 (0)