-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
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 tohome. The user tries to navigate toadmin, but he's redirected back tohomesince 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.