Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows redirect method to return same location #2773

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/async_redirection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/example/lib/redirection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions packages/go_router/lib/src/redirection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ FutureOr<RouteMatchList> redirect(
{List<RouteMatchList>? redirectHistory,
Object? extra}) {
FutureOr<RouteMatchList> processRedirect(RouteMatchList prevMatchList) {
final String prevLocation = prevMatchList.location.toString();
FutureOr<RouteMatchList> processTopLevelRedirect(
String? topRedirectLocation) {
if (topRedirectLocation != null) {
if (topRedirectLocation != null && topRedirectLocation != prevLocation) {
final RouteMatchList newMatch = _getNewMatches(
topRedirectLocation,
prevMatchList.location,
Expand Down Expand Up @@ -62,7 +63,8 @@ FutureOr<RouteMatchList> redirect(
}
FutureOr<RouteMatchList> processRouteLevelRedirect(
String? routeRedirectLocation) {
if (routeRedirectLocation != null) {
if (routeRedirectLocation != null &&
routeRedirectLocation != prevLocation) {
final RouteMatchList newMatch = _getNewMatches(
routeRedirectLocation,
prevMatchList.location,
Expand Down Expand Up @@ -102,7 +104,7 @@ FutureOr<RouteMatchList> 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
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
31 changes: 31 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,37 @@ void main() {
expect(redirected, isTrue);
});

testWidgets('redirect can redirect to same path',
(WidgetTester tester) async {
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) =>
const HomeScreen(),
routes: <GoRoute>[
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<GoRoute> routes = <GoRoute>[
Expand Down