Skip to content

Conversation

tomassasovsky
Copy link
Contributor

@tomassasovsky tomassasovsky commented Jul 30, 2025

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:

  /// Block navigation by redirecting to current location
  void block({bool reevaluateOnChange = false}) {
    if (isResolved) return;
    final currentLocation =
        GoRouter.of(context).routerDelegate.currentConfiguration.fullPath;
    // Block by redirecting to current location
    _completer.complete(GuardResult.redirect(
      currentLocation,
      reevaluateOnChange: reevaluateOnChange,
    ));
  }

This method, however, needs this PR because it's ran on the redirect function of a GoRouteData.

Pre-Review Checklist

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

  1. 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

Copy link

@gemini-code-assist gemini-code-assist bot left a 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!

@stuartmorgan-g
Copy link
Collaborator

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.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft July 31, 2025 12:12
@tomassasovsky tomassasovsky changed the title feat: access GoRouter.of from redirect methods [go_router] feat: access GoRouter.of from redirect methods Jul 31, 2025
@tomassasovsky tomassasovsky marked this pull request as ready for review July 31, 2025 16:25
@tomassasovsky
Copy link
Contributor Author

Thanks @stuartmorgan-g! Just updated and marked as ready.

Copy link
Contributor

@chunhtai chunhtai left a 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

@tomassasovsky tomassasovsky requested a review from chunhtai August 1, 2025 20:21
expect(redirected, isTrue);
});

testWidgets('GoRouter.of(context) should work in redirects',
Copy link
Contributor

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?

Copy link
Contributor Author

@tomassasovsky tomassasovsky Aug 5, 2025

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.

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai
Copy link
Contributor

looks like there is some conflict and ci error

@tomassasovsky
Copy link
Contributor Author

I'm not too sure about what's failing here @chunhtai - I made sure everything's formatted correctly so it doesn't fail, but it still is.

@chunhtai
Copy link
Contributor

looks like you may be using an outdated formatter. can you make sure you have latest flutter and also run flutter pub dev in go_router package?

@tomassasovsky tomassasovsky force-pushed the feat/go-router-context-redirect branch from 658f181 to 05c1cbc Compare September 25, 2025 02:47
@tomassasovsky
Copy link
Contributor Author

All fixed now, thanks @chunhtai :)

@chunhtai chunhtai requested a review from hannah-hyj October 7, 2025 22:33
Copy link
Member

@hannah-hyj hannah-hyj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tomassasovsky
Copy link
Contributor Author

any updates on this? @chunhtai

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 14, 2025
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 14, 2025
Copy link
Contributor

auto-submit bot commented Oct 14, 2025

autosubmit label was removed for flutter/packages/9706, because - The status or check suite Mac_arm64 ios_platform_tests_shard_5 master has failed. Please fix the issues identified (or deflake) before re-applying this label.

  • The status or check suite Mac_arm64 macos_repo_checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 14, 2025
@auto-submit auto-submit bot merged commit 9084aeb into flutter:main Oct 15, 2025
80 checks passed
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Oct 16, 2025
flutter/packages@d062181...835dccb

2025-10-16 [email protected] [local_auth] Adopt structured errors
and remove useErrorDialogs - platform implementations
(flutter/packages#10147)
2025-10-16 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump org.mockito.kotlin:mockito-kotlin from 6.0.0 to 6.1.0
in /packages/interactive_media_ads/android (flutter/packages#10221)
2025-10-15 [email protected] Manual roll Flutter from
e11e2c1 to 7cd821c (73 revisions) (flutter/packages#10229)
2025-10-15 [email protected] [go_router] feat: access GoRouter.of
from redirect methods (flutter/packages#9706)
2025-10-14 [email protected] [Many] Migrate non
examples (and pigeon test) to java 17 (flutter/packages#10201)
2025-10-13 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.google.ads.interactivemedia.v3:interactivemedia
from 3.36.0 to 3.37.0 in /packages/interactive_media_ads/android
(flutter/packages#10113)
2025-10-13 [email protected] [pigeon]
Fixes support for Kotlin/Java classes that override equals and hashCode
for ProxyApis (flutter/packages#10039)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Oct 16, 2025
flutter/packages@d062181...835dccb

2025-10-16 [email protected] [local_auth] Adopt structured errors
and remove useErrorDialogs - platform implementations
(flutter/packages#10147)
2025-10-16 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump org.mockito.kotlin:mockito-kotlin from 6.0.0 to 6.1.0
in /packages/interactive_media_ads/android (flutter/packages#10221)
2025-10-15 [email protected] Manual roll Flutter from
e11e2c1 to 7cd821c (73 revisions) (flutter/packages#10229)
2025-10-15 [email protected] [go_router] feat: access GoRouter.of
from redirect methods (flutter/packages#9706)
2025-10-14 [email protected] [Many] Migrate non
examples (and pigeon test) to java 17 (flutter/packages#10201)
2025-10-13 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.google.ads.interactivemedia.v3:interactivemedia
from 3.36.0 to 3.37.0 in /packages/interactive_media_ads/android
(flutter/packages#10113)
2025-10-13 [email protected] [pigeon]
Fixes support for Kotlin/Java classes that override equals and hashCode
for ProxyApis (flutter/packages#10039)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: go_router

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants