River_pod: Usage of watch. Documentation shows 2 different usage of watch

Created on 9 Oct 2020  路  3Comments  路  Source: rrousselGit/river_pod

Documentation shows 2 different usage of watch:
watch(xxxProvider.state) and watch(xxxProvider).state , is there any difference? clarify when to use which?

documentation

Most helpful comment

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.

All 3 comments

Documentation shows 2 different usage of watch:
watch(xxxProvider.state) and watch(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.

Was this page helpful?
0 / 5 - 0 ratings