1

To access the state of a StateProvider or StateNotifierProvider:

Sometimes in the Riverpod documentation, the state variable is added after the watch function.

int count = watch(counterProvider).state;

However, my code where I am using a StateNotifier, works only if I refer to it inside watch. i.e

watch(myNotifier.state)

What are the differences?

joe kletz
  • 13
  • 3
  • Does this answer your question? [Flutter changing Text value using RiverPod State management](https://stackoverflow.com/questions/64341414/flutter-changing-text-value-using-riverpod-state-management) – Alex Hartford Mar 10 '21 at 16:27

1 Answers1

3

The widget that is consuming the provider will behave differently in the two cases.

In the first case:

watch(counterProvider).state

The consumer will look at the entire counterProvider and it will be rebuilt if anything causes a NotifyProvider.

In second case:

watch(counterProvider.state)

The consumer is looking at the state variable only and it will only be rebuilt if the state changes and cause a NotifyProvider.

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47