Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Riverpod does not catch List<Map>'s nested value changes

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

in riverpod’s documentation

final filter = [...ref.read(filterProvider)]; // Copies the list.
filter[i]['onOrOff'] = onOffVal;
ref.read(filterProvider.notifier).state = filter;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading