watch<N extends BaseNotifier<T>, T, R> abstract method

R watch<N extends BaseNotifier<T>, T, R>(
  1. BaseWatchable<N, T, R> watchable, {
  2. ListenerCallback<T>? listener,
  3. bool rebuildWhen(
    1. T prev,
    2. T next
    )?,
})

Get the current value of a provider and listen to changes. The listener will be disposed automatically when the widget is disposed.

Optionally, you can pass a rebuildWhen function to control when the widget should rebuild.

Instead of ref.watch(provider), you can also use ref.watch(provider.select((state) => state.attribute)) to select a part of the state and only rebuild when this part changes.

Do NOT execute this method multiple times as only the last one will be used for the rebuild condition. Instead, you should use Records to combine multiple values: final (a, b) = ref.watch(provider.select((state) => (state.a, state.b)));

Only call this method during build or inside a ViewProvider.

Implementation

R watch<N extends BaseNotifier<T>, T, R>(
  BaseWatchable<N, T, R> watchable, {
  ListenerCallback<T>? listener,
  bool Function(T prev, T next)? rebuildWhen,
});