final filterProvider = StateProvider<List<Map>>((ref) => [
{'text': 'rating', 'onOrOff': true},
{'text': 'km', 'onOrOff': true},
]);
I have this filterProvider. And I am trying to update the value of ‘onOrOff’.
ref.watch(filterProvider.notifier).state[i]['onOrOff'] = onOffVal;
When I do this way, Riverpod does not change the value immediately, how can I adjust the value and make Riverpod to apply changes right away?
>Solution :
In riverpod the state provided with a StateProvider is supposed to be immutable. You cannot modify it, you need to re-assign it.
You can read
It is typically used for:
- exposing an immutable state which can change over time after reacting to custom events.
- centralizing the logic for modifying some state (aka "business logic") in a single place, improving maintainability over time.
final filter = [...ref.read(filterProvider)]; // Copies the list.
filter[i]['onOrOff'] = onOffVal;
ref.read(filterProvider.notifier).state = filter;