From d888abdefd4477b7b73956c15354bad1b90b8961 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Thu, 3 Nov 2022 11:13:13 -0700 Subject: [PATCH] Allows redirect method to return same location (#2773) * Allows redirect method to return same location * format and bump version --- packages/go_router/CHANGELOG.md | 6 +++- .../example/lib/async_redirection.dart | 2 +- .../go_router/example/lib/redirection.dart | 2 +- packages/go_router/lib/src/redirection.dart | 8 +++-- packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 31 +++++++++++++++++++ 6 files changed, 44 insertions(+), 7 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index aea6f3a6f1f4..3c130bc06cd3 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,7 +1,11 @@ +## 5.1.3 + +- Allows redirection to return same location. + ## 5.1.2 - Exposes uri and path parameters from GoRouter and fixes its notifications. -- Updates README +- Updates README. - Removes dynamic calls in examples. ## 5.1.1 diff --git a/packages/go_router/example/lib/async_redirection.dart b/packages/go_router/example/lib/async_redirection.dart index 14598a0e383d..b7a1545996c7 100644 --- a/packages/go_router/example/lib/async_redirection.dart +++ b/packages/go_router/example/lib/async_redirection.dart @@ -57,7 +57,7 @@ class App extends StatelessWidget { final bool loggedIn = await StreamAuthScope.of(context).isSignedIn(); final bool loggingIn = state.subloc == '/login'; if (!loggedIn) { - return loggingIn ? null : '/login'; + return '/login'; } // if the user is logged in but still on the login page, send them to diff --git a/packages/go_router/example/lib/redirection.dart b/packages/go_router/example/lib/redirection.dart index 1e2357377985..4ce8253dd265 100644 --- a/packages/go_router/example/lib/redirection.dart +++ b/packages/go_router/example/lib/redirection.dart @@ -77,7 +77,7 @@ class App extends StatelessWidget { final bool loggedIn = _loginInfo.loggedIn; final bool loggingIn = state.subloc == '/login'; if (!loggedIn) { - return loggingIn ? null : '/login'; + return '/login'; } // if the user is logged in but still on the login page, send them to diff --git a/packages/go_router/lib/src/redirection.dart b/packages/go_router/lib/src/redirection.dart index 9f68716ebac5..996aa346083d 100644 --- a/packages/go_router/lib/src/redirection.dart +++ b/packages/go_router/lib/src/redirection.dart @@ -26,9 +26,10 @@ FutureOr redirect( {List? redirectHistory, Object? extra}) { FutureOr processRedirect(RouteMatchList prevMatchList) { + final String prevLocation = prevMatchList.location.toString(); FutureOr processTopLevelRedirect( String? topRedirectLocation) { - if (topRedirectLocation != null) { + if (topRedirectLocation != null && topRedirectLocation != prevLocation) { final RouteMatchList newMatch = _getNewMatches( topRedirectLocation, prevMatchList.location, @@ -62,7 +63,8 @@ FutureOr redirect( } FutureOr processRouteLevelRedirect( String? routeRedirectLocation) { - if (routeRedirectLocation != null) { + if (routeRedirectLocation != null && + routeRedirectLocation != prevLocation) { final RouteMatchList newMatch = _getNewMatches( routeRedirectLocation, prevMatchList.location, @@ -102,7 +104,7 @@ FutureOr redirect( context, GoRouterState( configuration, - location: prevMatchList.location.toString(), + location: prevLocation, name: null, // No name available at the top level trim the query params off the // sub-location to match route.redirect diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index c3151361c9f3..3c63da219224 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: 5.1.2 +version: 5.1.3 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 e10f11a8d32b..96df8e7af5da 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -1208,6 +1208,37 @@ void main() { expect(redirected, isTrue); }); + testWidgets('redirect can redirect to same path', + (WidgetTester tester) async { + final List routes = [ + GoRoute( + path: '/', + builder: (BuildContext context, GoRouterState state) => + const HomeScreen(), + routes: [ + GoRoute( + path: 'dummy', + // Return same location. + redirect: (_, GoRouterState state) => state.location, + builder: (BuildContext context, GoRouterState state) => + const DummyScreen()), + ], + ), + ]; + + final GoRouter router = await createRouter(routes, tester, + redirect: (BuildContext context, GoRouterState state) { + // Return same location. + return state.location; + }); + + expect(router.location, '/'); + // Directly set the url through platform message. + await sendPlatformUrl('/dummy'); + await tester.pumpAndSettle(); + expect(router.location, '/dummy'); + }); + testWidgets('top-level redirect w/ named routes', (WidgetTester tester) async { final List routes = [