diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 3c9ce2c40b33..f1b1b261713e 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -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. diff --git a/packages/go_router/lib/src/parser.dart b/packages/go_router/lib/src/parser.dart index 27485f088370..dc70baa16b2b 100644 --- a/packages/go_router/lib/src/parser.dart +++ b/packages/go_router/lib/src/parser.dart @@ -83,6 +83,9 @@ class GoRouteInformationParser extends RouteInformationParser { 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) { diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 2e90474be89b..f7f8e2b0f3aa 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -526,8 +526,10 @@ class GoRouter implements RouterConfig { // 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 == '/') { diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index b7c93c1ad889..dbf0f854a33a 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -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 diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index e486129b201f..d2b3e91a0025 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -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', () { @@ -2466,6 +2466,45 @@ void main() { }); }); + group('_effectiveInitialLocation()', () { + final List routes = [ + 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 routes = [