skip to Main Content

Using go_router :

GoRouter.of(context).location

gives us the current route path such as /product/10110 but I’d like to know how to also get the current route queryParams in a similar fashion

(outside of the GoRoute builder)

2

Answers


  1. go_router has it’s qweryParams in its state.

    Hence pass the state to the page


    Router

     GoRoute(
        name: "test",
        path: "/test",
        builder: (context, state) {
          return SampleWidget(
            goRouterState: state,  👈 Pass state here
          );
        },
      ),
    

    Usage

    context.goNamed("test", queryParams: {"email": "[email protected]", "age": "25"}),
    

    Accesing in the page

    class SampleWidget extends StatelessWidget {
      GoRouterState? goRouterState;
      SampleWidget({super.key, this.goRouterState});
    
      @override
      Widget build(BuildContext context) {
        print(goRouterState?.queryParams.toString()); 👈 access anywhere like so
    
        return const Scaffold(
          body: ...
        );
      }
    }
    
    Login or Signup to reply.
  2. You can now have this functionality.

    class SampleWidget extends StatelessWidget {
      SampleWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        Map<String,dynamic> qparams = GoRouterState.of(context).queryParams;
    
        return const Scaffold(
          body: ...
        );
      }
    }
    

    This way you can directly access the query params you send using go_router, like:

    context.goNamed("page", queryParams: {"name": "Addy", "age": "22"}),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search