From 5929d660a0e190d4c15d30571e978a2366a3742c Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Fri, 10 Nov 2023 01:01:30 +0800 Subject: [PATCH] [go_router] Retain query parameter during refresh and first redirect (#5276) Fixes https://github.com/flutter/flutter/issues/137513 --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router/lib/src/router.dart | 16 ++++++++++++---- packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 63e9cdbf4ba9..fe4b38e06364 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 12.1.1 + +- Retains query parameters during refresh and first redirect. + ## 12.1.0 - Adds an ability to add a custom codec for serializing/deserializing extra. diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 0f27dcf48c40..dc6d88057eb0 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -531,10 +531,18 @@ class GoRouter implements RouterConfig { // verified by assert() during the initialization. return initialLocation!; } - final Uri platformDefaultUri = - Uri.parse(WidgetsBinding.instance.platformDispatcher.defaultRouteName); - final String platformDefault = - platformDefaultUri.path.isEmpty ? '/' : platformDefaultUri.path; + Uri platformDefaultUri = Uri.parse( + WidgetsBinding.instance.platformDispatcher.defaultRouteName, + ); + if (platformDefaultUri.hasEmptyPath) { + // TODO(chunhtai): Clean up this once `RouteInformation.uri` is available + // in packages repo. + platformDefaultUri = Uri( + path: '/', + queryParameters: platformDefaultUri.queryParameters, + ); + } + final String platformDefault = platformDefaultUri.toString(); if (initialLocation == null) { return platformDefault; } else if (platformDefault == '/') { diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index f37ba325b304..7247f2a3cdf1 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.1.0 +version: 12.1.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 213b27ab60e9..e338aeee65e7 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -2503,6 +2503,21 @@ void main() { TestWidgetsFlutterBinding.instance.platformDispatcher .clearDefaultRouteNameTestValue(); }); + + testWidgets( + 'When platformDispatcher.defaultRouteName is deep-link Uri with ' + 'scheme, authority, no path, and query parameters', + (WidgetTester tester) async { + TestWidgetsFlutterBinding.instance.platformDispatcher + .defaultRouteNameTestValue = 'https://domain.com?param=1'; + final GoRouter router = await createRouter( + routes, + tester, + ); + expect(router.routeInformationProvider.value.uri.toString(), '/?param=1'); + TestWidgetsFlutterBinding.instance.platformDispatcher + .clearDefaultRouteNameTestValue(); + }); }); group('params', () {