I've been trying to find out what is the difference between watching a StateNotifierPovider
object vs watching the notifier exposed by it. As we can see in the docs following section watching the notifier object does not trigger the object's build method when the state is changed. Out of experimenting, it seems like watching the provider object exposes the state within the notifier just as ref.read(provider.notifier).state
would. I can't really get the difference between directly watching the provider vs provider.notifier
and why it does not trigger the build method when watching the notifier and changing its state.
Asked
Active
Viewed 2,213 times
6

FRANCISCO BERNAD
- 473
- 4
- 15
1 Answers
14
The documentation of .notifier
should hopefully be clear about it:
Obtains the StateNotifier associated with this provider, without listening to state changes.
This is typically used to invoke methods on a StateNotifier. For example:
Button( onTap: () => ref.read(stateNotifierProvider.notifer).increment(), )
This listenable will notify its notifiers if the StateNotifier instance changes. This may happen if the provider is refreshed or one of its dependencies has changes.
So ref.watch(provider)
listens to state changes.
And ref.watch(provider.notifier)
obtains the notifier only but does not listen to state change. Instead it listens to when the StateNotifier
instance is recreated – such as if you did ref.refresh(provider)
, which would recreate the StateNotifier

Rémi Rousselet
- 256,336
- 79
- 519
- 432
-
1Awesome Remi! A few minutes ago I was digging the api docs and stumbled upon this documentation. Same about the refresh/invalidate methods (maybe this could be added to the v2 of the riverpod docs). Big fan of yours by the way, thanks for everything you've been doing for the flutter/dart community! – FRANCISCO BERNAD Nov 16 '22 at 21:34
-
1Im a simple man I see Remi I upvote. – 最白目 Mar 22 '23 at 07:47
-
Kindly explain when to use `ref.read(provider.notifier)` vs `ref.watch(provider.notifier)` inside callback functions(like onPressed) when not watching for state change but to modify state. Pl. take both cases of using them directly in functions compared to using their reference in functions, collected earlier in build method. – Johny Gates Aug 19 '23 at 10:09