How to update riverpod from GoRouter?

592 Views Asked by At

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

There are 1 best solutions below

2
On

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.