Skip to content

Commit

Permalink
[go_router] Fixes deep-link with no path on cold start (#5113)
Browse files Browse the repository at this point in the history
*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
  • Loading branch information
hrishikesh-kadam authored Oct 27, 2023
1 parent 64ae77f commit 6bcb747
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 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 @@
## 12.0.1

- Fixes deep-link with no path on cold start.

## 12.0.0

- Adds ability to dynamically update routing table.
Expand Down
3 changes: 3 additions & 0 deletions packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
initialMatches =
// TODO(chunhtai): remove this ignore and migrate the code
// https://github.com/flutter/flutter/issues/124045.
// TODO(chunhtai): After the migration from routeInformation's location
// to uri, empty path check might be required here; see
// https://github.com/flutter/packages/pull/5113#discussion_r1374861070
// ignore: deprecated_member_use, unnecessary_non_null_assertion
configuration.findMatch(routeInformation.location!, extra: state.extra);
if (initialMatches.isError) {
Expand Down
4 changes: 3 additions & 1 deletion packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,10 @@ class GoRouter implements RouterConfig<RouteMatchList> {
// verified by assert() during the initialization.
return initialLocation!;
}
final Uri platformDefaultUri =
Uri.parse(WidgetsBinding.instance.platformDispatcher.defaultRouteName);
final String platformDefault =
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
platformDefaultUri.path.isEmpty ? '/' : platformDefaultUri.path;
if (initialLocation == null) {
return platformDefault;
} else if (platformDefault == '/') {
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: 12.0.0
version: 12.0.1
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
43 changes: 41 additions & 2 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2445,8 +2445,8 @@ void main() {
// https://github.com/flutter/flutter/issues/124045.
// ignore: deprecated_member_use
expect(router.routeInformationProvider.value.location, '/dummy');
TestWidgetsFlutterBinding
.instance.platformDispatcher.defaultRouteNameTestValue = '/';
TestWidgetsFlutterBinding.instance.platformDispatcher
.clearDefaultRouteNameTestValue();
});

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

group('_effectiveInitialLocation()', () {
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) =>
const HomeScreen(),
),
];

testWidgets(
'When platformDispatcher.defaultRouteName is deep-link Uri with '
'scheme, authority, no path', (WidgetTester tester) async {
TestWidgetsFlutterBinding.instance.platformDispatcher
.defaultRouteNameTestValue = 'https://domain.com';
final GoRouter router = await createRouter(
routes,
tester,
);
expect(router.routeInformationProvider.value.uri.path, '/');
TestWidgetsFlutterBinding.instance.platformDispatcher
.clearDefaultRouteNameTestValue();
});

testWidgets(
'When platformDispatcher.defaultRouteName is deep-link Uri with '
'scheme, authority, no path, but trailing slash',
(WidgetTester tester) async {
TestWidgetsFlutterBinding.instance.platformDispatcher
.defaultRouteNameTestValue = 'https://domain.com/';
final GoRouter router = await createRouter(
routes,
tester,
);
expect(router.routeInformationProvider.value.uri.path, '/');
TestWidgetsFlutterBinding.instance.platformDispatcher
.clearDefaultRouteNameTestValue();
});
});

group('params', () {
testWidgets('preserve path param case', (WidgetTester tester) async {
final List<GoRoute> routes = <GoRoute>[
Expand Down

0 comments on commit 6bcb747

Please sign in to comment.