3

I am using riverpod package for state management in a Flutter app and I have a search sort provider as follows:

final searchSortStateProvider = StateProvider<SearchSort?>((ref) {   return SearchSort.relevance; });

But when the user searches again I want to reset the state of the search sort provider using the following code:

 ref.read(searchSortStateProvider.notifier).state = SearchSort.relevance;

It throws:

"setState() or markNeedsBuild called during build"

Exception because I call it inside the init() method of the parent widget.

My question is how can I change the state of the provider without needing to rebuild the sort widget?

Simple Code
  • 2,354
  • 2
  • 27
  • 56

1 Answers1

1

maybe you try ref.refresh or ref.invalidate/ref.invalidateSelf()?

Ruble
  • 2,589
  • 3
  • 6
  • 29