Skip to content

Commit 6bcb747

Browse files
[go_router] Fixes deep-link with no path on cold start (#5113)
*List which issues are fixed by this PR. You must list at least one issue.* - flutter/flutter#133928 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* - No breaking changes
1 parent 64ae77f commit 6bcb747

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
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+
## 12.0.1
2+
3+
- Fixes deep-link with no path on cold start.
4+
15
## 12.0.0
26

37
- Adds ability to dynamically update routing table.

packages/go_router/lib/src/parser.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
8383
initialMatches =
8484
// TODO(chunhtai): remove this ignore and migrate the code
8585
// https://github.com/flutter/flutter/issues/124045.
86+
// TODO(chunhtai): After the migration from routeInformation's location
87+
// to uri, empty path check might be required here; see
88+
// https://github.com/flutter/packages/pull/5113#discussion_r1374861070
8689
// ignore: deprecated_member_use, unnecessary_non_null_assertion
8790
configuration.findMatch(routeInformation.location!, extra: state.extra);
8891
if (initialMatches.isError) {

packages/go_router/lib/src/router.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,10 @@ class GoRouter implements RouterConfig<RouteMatchList> {
526526
// verified by assert() during the initialization.
527527
return initialLocation!;
528528
}
529+
final Uri platformDefaultUri =
530+
Uri.parse(WidgetsBinding.instance.platformDispatcher.defaultRouteName);
529531
final String platformDefault =
530-
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
532+
platformDefaultUri.path.isEmpty ? '/' : platformDefaultUri.path;
531533
if (initialLocation == null) {
532534
return platformDefault;
533535
} else if (platformDefault == '/') {

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: 12.0.0
4+
version: 12.0.1
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,8 +2445,8 @@ void main() {
24452445
// https://github.com/flutter/flutter/issues/124045.
24462446
// ignore: deprecated_member_use
24472447
expect(router.routeInformationProvider.value.location, '/dummy');
2448-
TestWidgetsFlutterBinding
2449-
.instance.platformDispatcher.defaultRouteNameTestValue = '/';
2448+
TestWidgetsFlutterBinding.instance.platformDispatcher
2449+
.clearDefaultRouteNameTestValue();
24502450
});
24512451

24522452
test('throws assertion if initialExtra is set w/o initialLocation', () {
@@ -2466,6 +2466,45 @@ void main() {
24662466
});
24672467
});
24682468

2469+
group('_effectiveInitialLocation()', () {
2470+
final List<GoRoute> routes = <GoRoute>[
2471+
GoRoute(
2472+
path: '/',
2473+
builder: (BuildContext context, GoRouterState state) =>
2474+
const HomeScreen(),
2475+
),
2476+
];
2477+
2478+
testWidgets(
2479+
'When platformDispatcher.defaultRouteName is deep-link Uri with '
2480+
'scheme, authority, no path', (WidgetTester tester) async {
2481+
TestWidgetsFlutterBinding.instance.platformDispatcher
2482+
.defaultRouteNameTestValue = 'https://domain.com';
2483+
final GoRouter router = await createRouter(
2484+
routes,
2485+
tester,
2486+
);
2487+
expect(router.routeInformationProvider.value.uri.path, '/');
2488+
TestWidgetsFlutterBinding.instance.platformDispatcher
2489+
.clearDefaultRouteNameTestValue();
2490+
});
2491+
2492+
testWidgets(
2493+
'When platformDispatcher.defaultRouteName is deep-link Uri with '
2494+
'scheme, authority, no path, but trailing slash',
2495+
(WidgetTester tester) async {
2496+
TestWidgetsFlutterBinding.instance.platformDispatcher
2497+
.defaultRouteNameTestValue = 'https://domain.com/';
2498+
final GoRouter router = await createRouter(
2499+
routes,
2500+
tester,
2501+
);
2502+
expect(router.routeInformationProvider.value.uri.path, '/');
2503+
TestWidgetsFlutterBinding.instance.platformDispatcher
2504+
.clearDefaultRouteNameTestValue();
2505+
});
2506+
});
2507+
24692508
group('params', () {
24702509
testWidgets('preserve path param case', (WidgetTester tester) async {
24712510
final List<GoRoute> routes = <GoRoute>[

0 commit comments

Comments
 (0)