Skip to content
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

[go_router] Fixes RouteInformationParser that does not restore full RouteMatchList if the optionURLReflectsImperativeAPIs is set #4713

Merged
merged 3 commits into from
Sep 18, 2023
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
5 changes: 5 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 10.1.4

- Fixes RouteInformationParser that does not restore full RouteMatchList if
the optionURLReflectsImperativeAPIs is set.

## 10.1.3

- Fixes an issue in the documentation that was using `state.queryParameters` instead of `state.uri.queryParameters`.
Expand Down
11 changes: 8 additions & 3 deletions packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,21 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
if (configuration.isEmpty) {
return null;
}
final String location;
if (GoRouter.optionURLReflectsImperativeAPIs &&
configuration.matches.last is ImperativeRouteMatch) {
configuration =
(configuration.matches.last as ImperativeRouteMatch).matches;
location = (configuration.matches.last as ImperativeRouteMatch)
.matches
.uri
.toString();
} else {
location = configuration.uri.toString();
}
return RouteInformation(
// TODO(chunhtai): remove this ignore and migrate the code
// https://github.com/flutter/flutter/issues/124045.
// ignore: deprecated_member_use
location: configuration.uri.toString(),
location: location,
state: _routeMatchListCodec.encode(configuration),
);
}
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: 10.1.3
version: 10.1.4
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
5 changes: 2 additions & 3 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,10 @@ void main() {
final RouteMatchListCodec codec =
RouteMatchListCodec(router.configuration);
await tester.pumpAndSettle();
final ImperativeRouteMatch match = router
.routerDelegate.currentConfiguration.last as ImperativeRouteMatch;
expect(log, <Object>[
isMethodCall('selectMultiEntryHistory', arguments: null),
IsRouteUpdateCall('/settings', false, codec.encode(match.matches)),
IsRouteUpdateCall('/settings', false,
codec.encode(router.routerDelegate.currentConfiguration)),
]);
GoRouter.optionURLReflectsImperativeAPIs = false;
});
Expand Down
49 changes: 49 additions & 0 deletions packages/go_router/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';

import 'test_helpers.dart';

RouteInformation createRouteInformation(String location, [Object? extra]) {
return RouteInformation(
// TODO(chunhtai): remove this ignore and migrate the code
Expand Down Expand Up @@ -81,6 +83,53 @@ void main() {
expect(matches[1].route, routes[0].routes[0]);
});

testWidgets(
'GoRouteInformationParser can restore full route matches if optionURLReflectsImperativeAPIs is true',
(WidgetTester tester) async {
final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (_, __) => const Placeholder(),
routes: <GoRoute>[
GoRoute(
path: 'abc',
builder: (_, __) => const Placeholder(),
),
],
),
];
GoRouter.optionURLReflectsImperativeAPIs = true;
final GoRouter router =
await createRouter(routes, tester, navigatorKey: navKey);

// Generate RouteMatchList with imperative route match
router.go('/abc');
await tester.pumpAndSettle();
router.push('/');
await tester.pumpAndSettle();
final RouteMatchList matchList = router.routerDelegate.currentConfiguration;
expect(matchList.uri.toString(), '/abc');
expect(matchList.matches.length, 3);

final RouteInformation restoredRouteInformation =
router.routeInformationParser.restoreRouteInformation(matchList)!;
// URL reflects the latest push.
// TODO(chunhtai): remove this ignore and migrate the code
// https://github.com/flutter/flutter/issues/124045.
// ignore: deprecated_member_use
expect(restoredRouteInformation.location, '/');

// Can restore back to original RouteMatchList.
final RouteMatchList parsedRouteMatch = await router.routeInformationParser
.parseRouteInformationWithDependencies(
restoredRouteInformation, navKey.currentContext!);
expect(parsedRouteMatch.uri.toString(), '/abc');
expect(parsedRouteMatch.matches.length, 3);

GoRouter.optionURLReflectsImperativeAPIs = false;
});

test('GoRouteInformationParser can retrieve route by name', () async {
final List<GoRoute> routes = <GoRoute>[
GoRoute(
Expand Down