From 51813433fd9bf7d7dd5d4da540f3051e3797a9e2 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Fri, 16 Aug 2024 15:19:47 -0700 Subject: [PATCH 1/3] [go_router] Fixes replace and pushReplacement uri when only one route match in current route match list --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router/lib/src/parser.dart | 13 +++++++++--- packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 22 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 9374421558e0..519e547189ef 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 14.2.6 + +- Fixes replace and pushReplacement uri when only one route match in current route match list. + ## 14.2.5 - Fixes an issue where android back button pops pages in the wrong order. diff --git a/packages/go_router/lib/src/parser.dart b/packages/go_router/lib/src/parser.dart index b4115a1fca19..bac9eee89513 100644 --- a/packages/go_router/lib/src/parser.dart +++ b/packages/go_router/lib/src/parser.dart @@ -160,7 +160,6 @@ class GoRouteInformationParser extends RouteInformationParser { location = safeRoute.matches.uri.toString(); } } - return RouteInformation( uri: Uri.parse(location ?? configuration.uri.toString()), state: _routeMatchListCodec.encode(configuration), @@ -194,7 +193,11 @@ class GoRouteInformationParser extends RouteInformationParser { ); case NavigatingType.pushReplacement: final RouteMatch routeMatch = baseRouteMatchList!.last; - return baseRouteMatchList.remove(routeMatch).push( + baseRouteMatchList = baseRouteMatchList.remove(routeMatch); + if (baseRouteMatchList.isEmpty) { + return newMatchList; + } + return baseRouteMatchList.push( ImperativeRouteMatch( pageKey: _getUniqueValueKey(), completer: completer!, @@ -203,7 +206,11 @@ class GoRouteInformationParser extends RouteInformationParser { ); case NavigatingType.replace: final RouteMatch routeMatch = baseRouteMatchList!.last; - return baseRouteMatchList.remove(routeMatch).push( + baseRouteMatchList = baseRouteMatchList.remove(routeMatch); + if (baseRouteMatchList.isEmpty) { + return newMatchList; + } + return baseRouteMatchList.push( ImperativeRouteMatch( pageKey: routeMatch.pageKey, completer: completer!, diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 2ad6bd28e903..bef70c82d06b 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: 14.2.5 +version: 14.2.6 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 1a0effb663a4..29ba7ddd0b65 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -68,6 +68,28 @@ void main() { expect(find.byType(DummyScreen), findsOneWidget); }); + testWidgets('pushReplacement and replace when only one matches', + (WidgetTester tester) async { + final List routes = [ + GoRoute(name: '1', path: '/', builder: dummy), + GoRoute(name: '2', path: '/a', builder: dummy), + GoRoute(name: '3', path: '/b', builder: dummy), + ]; + + final GoRouter router = await createRouter(routes, tester); + expect(router.routerDelegate.currentConfiguration.uri.path, '/'); + + router.replace('/a'); + await tester.pumpAndSettle(); + // When the imperative match is the only match in the route match list, + // it should update the uri. + expect(router.routerDelegate.currentConfiguration.uri.path, '/a'); + + router.pushReplacement('/b'); + await tester.pumpAndSettle(); + expect(router.routerDelegate.currentConfiguration.uri.path, '/b'); + }); + test('empty path', () { expect(() { GoRoute(path: ''); From 4a1d9eeb68d6536395973867ee2c57abcfc01538 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Fri, 16 Aug 2024 15:20:46 -0700 Subject: [PATCH 2/3] format --- packages/go_router/lib/src/parser.dart | 24 ++++++++++----------- packages/go_router/test/go_router_test.dart | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/go_router/lib/src/parser.dart b/packages/go_router/lib/src/parser.dart index bac9eee89513..72f944b0c5a7 100644 --- a/packages/go_router/lib/src/parser.dart +++ b/packages/go_router/lib/src/parser.dart @@ -198,12 +198,12 @@ class GoRouteInformationParser extends RouteInformationParser { return newMatchList; } return baseRouteMatchList.push( - ImperativeRouteMatch( - pageKey: _getUniqueValueKey(), - completer: completer!, - matches: newMatchList, - ), - ); + ImperativeRouteMatch( + pageKey: _getUniqueValueKey(), + completer: completer!, + matches: newMatchList, + ), + ); case NavigatingType.replace: final RouteMatch routeMatch = baseRouteMatchList!.last; baseRouteMatchList = baseRouteMatchList.remove(routeMatch); @@ -211,12 +211,12 @@ class GoRouteInformationParser extends RouteInformationParser { return newMatchList; } return baseRouteMatchList.push( - ImperativeRouteMatch( - pageKey: routeMatch.pageKey, - completer: completer!, - matches: newMatchList, - ), - ); + ImperativeRouteMatch( + pageKey: routeMatch.pageKey, + completer: completer!, + matches: newMatchList, + ), + ); case NavigatingType.go: return newMatchList; case NavigatingType.restore: diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 29ba7ddd0b65..10e6bb58d5bd 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -69,7 +69,7 @@ void main() { }); testWidgets('pushReplacement and replace when only one matches', - (WidgetTester tester) async { + (WidgetTester tester) async { final List routes = [ GoRoute(name: '1', path: '/', builder: dummy), GoRoute(name: '2', path: '/a', builder: dummy), From 2a15e582e8d505a2620f840cb9ea69455781157a Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Tue, 20 Aug 2024 12:53:04 -0700 Subject: [PATCH 3/3] fix test --- packages/go_router/test/extension_test.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/go_router/test/extension_test.dart b/packages/go_router/test/extension_test.dart index 0f314edbf66e..c13a47de002f 100644 --- a/packages/go_router/test/extension_test.dart +++ b/packages/go_router/test/extension_test.dart @@ -38,9 +38,7 @@ void main() { final GoRouter router = await createGoRouter(tester); await tester.tap(find.text('Settings')); await tester.pumpAndSettle(); - final ImperativeRouteMatch routeMatch = router - .routerDelegate.currentConfiguration.last as ImperativeRouteMatch; - expect(routeMatch.matches.uri.toString(), + expect(router.routerDelegate.currentConfiguration.uri.toString(), '/page-0/settings?search=notification'); }); });