Skip to content

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

@lucavenir

Description

@lucavenir

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions