-
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 11 commits
5cea2f7
05b94b7
c93570b
c94be6f
858ea74
d2b6d00
d90201c
61bc8ff
6309387
fec0356
e756161
2cdaa91
52d78a7
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Symbol used as a Zone key to track the current GoRouter during redirects. | ||
@internal | ||
const Symbol currentRouterKey = #goRouterRedirectContext; |
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test that the error throw during the redirect can be caught by onException? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added proper exception handling in the What changed:
Why this matters: Works hand-in-hand with the configuration.dart fix to provide complete exception handling coverage throughout the redirect chain. |
||
(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.