-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[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?
[go_router] feat: access GoRouter.of from redirect methods #9706
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a great feature to access GoRouter.of(context)
from within redirect methods by using a Zone
. The implementation is solid and the new tests cover the functionality well.
I have a couple of suggestions to improve the code quality:
- I've identified a duplicated constant across two files and suggest centralizing it to improve maintainability.
- There's some redundant logic in the new
GoRouter.of
method which can be simplified.
Please see my detailed comments. Overall, great work!
Thanks for the contribution! You haven’t checked off any of the items in the PR checklist above, so I’m assuming this is a work in progress and am marking it as a Draft. Please review the checklist, updating the PR as appropriate, and when that’s complete please feel free to mark the PR as ready for review. |
Thanks @stuartmorgan-g! Just updated and marked as ready. |
…ovsky/packages into feat/go-router-context-redirect
…y usage during redirects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approach seems fine to me, just need some more error handling
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Added proper exception handling in the _redirect
method to ensure redirect errors are gracefully converted to error RouteMatchList
objects instead of crashing navigation.
What changed:
- Wrapped redirect calls in try-catch for sync exceptions
- Added
.catchError()
for async redirect exceptions - Both paths convert exceptions to
GoException
and return error match lists
Why this matters:
This ensures that when a redirect throws an exception (like in the failing test), it gets properly handled by the onException
callback instead of breaking the entire navigation flow. Previously, exceptions would bubble up and crash the router - now they're caught and transformed into proper error states that the router knows how to handle.
Works hand-in-hand with the configuration.dart fix to provide complete exception handling coverage throughout the redirect chain.
…ovsky/packages into feat/go-router-context-redirect
This PR fixes being able to access the GoRouter instance from the context inside redirect methods.
It solves flutter/flutter#116855 (comment)
My use case for this is:
I'm building an extension package for navigation guards for
go_router
. Specifically, for type-safe generated routes. These guards are chainable and need to have the ability to block a route push/go. For this, I have the following method implemented:This method, however, needs this PR because it's ran on the redirect function of a GoRouteData.
Pre-Review Checklist
[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assist
bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3