Documentation shows 2 different usage of watch:
watch(xxxProvider.state) and watch(xxxProvider).state , is there any difference? clarify when to use which?
Documentation shows 2 different usage of watch:
watch(xxxProvider.state)andwatch(xxxProvider).state, is there any difference? clarify when to use which?
If I am right. In this way you use when you want initialize.
final counter = watch(xxxProvider.state);
Second when you want watch you value in the UI without initialize.
Text('${watch(xxxProvider).state}' ),
But anyway you should get compile warning if you use it wrong.
Lets say you have a StateNotifierProvider,
Using final notifier = watch(xxxProvider); will create and return the StateNotifier instance but not its state. It will only rebuild the widget when the instance of notifier has been chaned.
Using final state = watch(xxxProvider.state); will create the StateNotifier and return the state of the StateNotifier. It will rebuild the widget whenever state has been changed.
That depends on the provider you are listening
With StateNotifierProvider, do watch(stateNotifierProvider.state)
With StateProvider do watch(stateProvider).state
I wish this could be improved. The issue here is the language.
Most helpful comment
Lets say you have a
StateNotifierProvider,Using
final notifier = watch(xxxProvider);will create and return the StateNotifier instance but not itsstate. It will only rebuild the widget when the instance ofnotifierhas been chaned.Using
final state = watch(xxxProvider.state);will create theStateNotifierand return thestateof theStateNotifier. It will rebuild the widget wheneverstatehas been changed.