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?
2
Answers
In
riverpod
thestate
provided with aStateProvider
is supposed to be immutable. You cannot modify it, you need to re-assign it.You can read
in riverpod’s documentation
This is part of why StateNotifier and StateProvider should not be used in modern Riverpod applications. Instead, you should be constructing a Notifier-based provider, and provide mutation methods to update your state. The Notifier can use combinations of assigning to state (as the previous StateNotifier did) and/or calling ref.notifyListeners() (as the previous ChangeNotifier did).
But please, stop using StateNotifier, ChangeNotifier, and StateProvider with Riverpod! They are all effectively deprecated.