From c157c6573ee483671ae1592dcaad76d3cbb8f4df Mon Sep 17 00:00:00 2001 From: yiiim Date: Sun, 29 Oct 2023 23:20:58 +0800 Subject: [PATCH 1/3] Fixes the problem what pathParameters is null in redirect when the Router is recreated. --- packages/go_router/CHANGELOG.md | 4 ++ packages/go_router/lib/src/configuration.dart | 9 +++-- packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 37 +++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index f1b1b261713..030c3f1d801 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 12.0.2 + +- Fixes the problem what pathParameters is null in redirect when the Router is recreated. + ## 12.0.1 - Fixes deep-link with no path on cold start. diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index 8e6393503a5..955c22ea846 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -495,17 +495,18 @@ class RouteConfiguration { final RouteBase route = match.route; FutureOr routeRedirectResult; if (route is GoRoute && route.redirect != null) { + final RouteMatchList effectiveMatchList = match is ImperativeRouteMatch ? match.matches : matchList; routeRedirectResult = route.redirect!( context, GoRouterState( this, - uri: matchList.uri, + uri: effectiveMatchList.uri, matchedLocation: match.matchedLocation, name: route.name, path: route.path, - fullPath: matchList.fullPath, - extra: matchList.extra, - pathParameters: matchList.pathParameters, + fullPath: effectiveMatchList.fullPath, + extra: effectiveMatchList.extra, + pathParameters: effectiveMatchList.pathParameters, pageKey: match.pageKey, ), ); diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index dbf0f854a33..dfce6f6feed 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.1 +version: 12.0.2 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 d2b3e91a002..1ee4d063c71 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5048,6 +5048,43 @@ void main() { expectedInitialRoute); }); }); + + testWidgets('test the pathParameters in redirect when the Router is recreated', + (WidgetTester tester) async{ + final GoRouter router = GoRouter( + initialLocation: '/foo', + routes: [ + GoRoute( + path: '/foo', + builder: dummy, + routes: [ + GoRoute( + path: ':id', + redirect: (_, GoRouterState state) { + expect(state.pathParameters['id'], isNotNull); + return null; + }, + builder: dummy, + ), + ], + ), + ], + ); + await tester.pumpWidget( + MaterialApp.router( + key: UniqueKey(), + routerConfig: router, + ), + ); + router.push('/foo/123'); + await tester.pump(); // wait reportRouteInformation + await tester.pumpWidget( + MaterialApp.router( + key: UniqueKey(), + routerConfig: router, + ), + ); + }); } class TestInheritedNotifier extends InheritedNotifier> { From 3b297704847f487ea7aac239c67467c9a5349b71 Mon Sep 17 00:00:00 2001 From: yiiim Date: Sun, 29 Oct 2023 23:48:08 +0800 Subject: [PATCH 2/3] format code --- packages/go_router/lib/src/configuration.dart | 3 ++- packages/go_router/test/go_router_test.dart | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index 955c22ea846..16f38f3b271 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -495,7 +495,8 @@ class RouteConfiguration { final RouteBase route = match.route; FutureOr routeRedirectResult; if (route is GoRoute && route.redirect != null) { - final RouteMatchList effectiveMatchList = match is ImperativeRouteMatch ? match.matches : matchList; + final RouteMatchList effectiveMatchList = + match is ImperativeRouteMatch ? match.matches : matchList; routeRedirectResult = route.redirect!( context, GoRouterState( diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 1ee4d063c71..213b27ab60e 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5049,8 +5049,9 @@ void main() { }); }); - testWidgets('test the pathParameters in redirect when the Router is recreated', - (WidgetTester tester) async{ + testWidgets( + 'test the pathParameters in redirect when the Router is recreated', + (WidgetTester tester) async { final GoRouter router = GoRouter( initialLocation: '/foo', routes: [ From 27aa23f2fc1d37fc412fda46f3e5b861c2133e5a Mon Sep 17 00:00:00 2001 From: yim Date: Thu, 2 Nov 2023 09:29:30 +0800 Subject: [PATCH 3/3] Update packages/go_router/CHANGELOG.md Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 030c3f1d801..78fee03acac 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,6 +1,6 @@ ## 12.0.2 -- Fixes the problem what pathParameters is null in redirect when the Router is recreated. +- Fixes the problem that pathParameters is null in redirect when the Router is recreated. ## 12.0.1