2

In my GoRouter, I retrieve the query params from the URL:

builder: (context, state) {
  String q = state.queryParams['q'] ?? '';
  int pageNumberInt = int.tryParse(state.queryParams['page'] ?? '1') ?? 1;
  // Return component...
} 

I would like, before returning the Widget, to update the state of a provider but how do I get access to "ref" to do something like:

ref.watch(searchFieldValueProvider.notifier).update((state) => q)

1 Answers1

1

In any case, you need to have access to Ref in order to update the provider status. You can pass it as an argument or in a class constructor. It all depends on your architecture.

And, it will be ref.read(...) or ref.refresh(...) (if we just need to reset the state) that will need to be used.

Ruble
  • 2,589
  • 3
  • 6
  • 29
  • Thx but as I use a goRouter, how can I add it to the constructor ? and how to build ref to pass it to GoRouter ? final roylloRouter = GoRouter( routes: [...] – Stéphane Traumat Dec 27 '22 at 20:35
  • you can see an example implementation [here](https://github.com/lucavenir/go_router_riverpod). And check out this [discussion](https://github.com/rrousselGit/riverpod/discussions/1357) to pick up some useful ideas. – Ruble Dec 28 '22 at 07:35