In Riverpod, if you have a class that has dynamic state, but you don't need to display changes to the state in the UI, do you use a StateNotifierProvider
or just a plain Provider
? Because StateNotifierProvider
provides a hook to update the state of the class. What about if we manually update the state of a plain Provider
, like:
final personProvider = Provider<Person>((provider) {
final person = Person(name: 'Bob');
return person;
});
Then elsewhere we update its state:
ref.read(personProvider) = Person(name: 'Joe');
Then elsewhere we rely on that state but do not display it in the UI:
// Will this be Bob, or Joe?
final dataToSaveToDatabase = ref.watch(personProvider);
Will Riverpod keep track of the state changes over time, such that if somewhere else in the app accesses said state with watch
or read
, it always gets the latest state?
Another way to frame this question is, "Is a StateNotifierProvider
only used if you need to update the UI?"