[go_router_builder] GoRouteData `redirect` function has no context nor state parameters

This issue has been tracked since 2022-11-23.

TLDR

The pain point described here is that GoRouteData's redirect differs, or rather lacks, from GoRouter's. See GoRouter's redirect vs GoRouteData's redirect.

Without GoRouteData matching GoRouter 's redirect APIs, I am unable to inject state or to read the request sublocation and therefore I have to give up on typed routing.

The root of the problem arises from integrating GoRouter with Riverpod (or Provider for what matters).

Use case

I am venturing through go_router_builder and I am trying to make it work with my state management solution.
So far, so good.

Then, I tried implementing a sub-route with a route-level redirection. Say you have the following (simplified) routes:

+ splash
+ login
+ home
++ admin
++ dashboard
++ guest-view
Details of the use case

The user opens the app and sees a splash page. He is not logged in, and therefore should be redirected towards login. Then, the users chooses to login as a guest. Just like that, he's redirected to home. The user tries to navigate to admin, but he's redirected back to home since he has not enough permissions. Some sort of feedback is showed to the user.

As detailed above, the crucial part here is being able to write route-level logic that redirects an unauthenticated but not unauthorized user, e.g. a guest user trying to access an admin page. Without codegen, I usually do this via route-level redirects. I read some state via ref.read (Riverpod) and then make decisions based on those results.

Unluckily, this is not currently possible via GoRouteData and the codegen APIs. Or at least it looks like it.

Proposal

Let GoRouteData have a FutureOr<String?> Function(BuildContext, GoRouterState) redirect parameter instead of the current simple FutureOr<String?> redirect().

This allows the developer to inject state through BuildContext and use GoRouterState when needed.

exaby73 wrote this answer on 2022-11-24

Hello @lucavenir. Thank you for the proposal. Looks like a valid one to me. Labeling for visibility by the team

lucavenir wrote this answer on 2022-11-24

Thank you @exaby73. I want to underline how this behavior might be desirable with builder in GoRouteData. Note how that method lacks a GoRouterState parameter (which is normally found in GoRouter).

That might be de desirable, I guess that works, as we're defining GoRouteData to directly accept typed parameters in its subclass, and let go_router_builder generate those APIs for us.

But in case of redirect I don't think it's possible to write logic without BuildContext (i.e. we need global state and we need context to do so) or on GoRouterState (i.e. while I do have the parameters, I lack the information about the requested sublocation).

If there's a way to make that work, please let me know, i.e. this becomes a documentation request rather than a feature request.

More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-12-07
Star Count 147031
Watcher Count 3560
Fork Count 23915
Issue Count 11300

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date