2

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)

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
Aristidios
  • 1,921
  • 2
  • 13
  • 24

2 Answers2

3

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"}),
Aditya Arora
  • 351
  • 2
  • 7
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": "example@gmail.com", "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: ...
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • Right, I guess I was more wondering if I could more easily access it through GoRouter.of(context) but I'm guessing not ! Thanks for your answer I will up vote it :) – Aristidios Jan 04 '23 at 20:26
  • 1
    Currently it's not possible using GoRouter.of(context) , so this is the work around – krishnaacharyaa Jan 05 '23 at 01:15