-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[go_router] feat: access GoRouter.of from redirect methods #9706
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
5cea2f7
05b94b7
c93570b
c94be6f
858ea74
d2b6d00
d90201c
61bc8ff
6309387
fec0356
e756161
2cdaa91
52d78a7
6221028
05c1cbc
7d8d0d5
7beaf8e
1619f40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2238,6 +2238,61 @@ void main() { | |
expect(redirected, isTrue); | ||
}); | ||
|
||
testWidgets('GoRouter.of(context) should work in redirects', | ||
|
||
(WidgetTester tester) async { | ||
GoRouter? capturedRouter; | ||
final List<GoRoute> routes = <GoRoute>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const HomeScreen(), | ||
), | ||
GoRoute( | ||
path: '/login', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const LoginScreen(), | ||
), | ||
]; | ||
|
||
final GoRouter router = await createRouter(routes, tester, | ||
redirect: (BuildContext context, GoRouterState state) { | ||
// This should not throw an exception | ||
capturedRouter = GoRouter.of(context); | ||
return state.matchedLocation == '/login' ? null : '/login'; | ||
}); | ||
|
||
expect(capturedRouter, isNotNull); | ||
expect(capturedRouter, equals(router)); | ||
}); | ||
|
||
testWidgets('Context extension methods should work in redirects', | ||
(WidgetTester tester) async { | ||
String? capturedNamedLocation; | ||
final List<GoRoute> routes = <GoRoute>[ | ||
GoRoute( | ||
path: '/', | ||
name: 'home', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const HomeScreen(), | ||
), | ||
GoRoute( | ||
path: '/login', | ||
name: 'login', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const LoginScreen(), | ||
), | ||
]; | ||
|
||
await createRouter(routes, tester, | ||
redirect: (BuildContext context, GoRouterState state) { | ||
// This should not throw an exception | ||
capturedNamedLocation = context.namedLocation('login'); | ||
return state.matchedLocation == '/login' ? null : '/login'; | ||
}); | ||
|
||
expect(capturedNamedLocation, '/login'); | ||
}); | ||
|
||
testWidgets('redirect can redirect to same path', | ||
(WidgetTester tester) async { | ||
final List<GoRoute> routes = <GoRoute>[ | ||
|
Uh oh!
There was an error while loading. Please reload this page.