listen<T> method

  1. @override
ProviderSubscription<T> listen<T>(
  1. ProviderListenable<T> listenable,
  2. void listener(
    1. T? previous,
    2. T value
    ), {
  3. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  4. bool fireImmediately = false,
  5. void onDependencyMayHaveChanged()?,
})
override

Listen to a provider and call listener whenever its value changes.

Listeners will automatically be removed when the provider rebuilds (such as when a provider listened with watch changes).

Returns an object that allows cancelling the subscription early.

fireImmediately (false by default) can be optionally passed to tell Riverpod to immediately call the listener with the current value.

onError can be specified to listen to uncaught errors in the provider.
Note:
onError will not be triggered if the provider catches the exception and emit a valid value out of it. As such, if a FutureProvider/StreamProvider fail, onError will not be called. Instead the listener will receive an AsyncError.

Implementation

@override
ProviderSubscription<T> listen<T>(
  ProviderListenable<T> listenable,
  void Function(T? previous, T value) listener, {
  void Function(Object error, StackTrace stackTrace)? onError,
  bool fireImmediately = false,
  // Not part of the public "Ref" API
  void Function()? onDependencyMayHaveChanged,
}) {
  _assertNotOutdated();
  assert(!_debugIsRunningSelector, 'Cannot call ref.read inside a selector');
  assert(_debugAssertCanDependOn(listenable), '');

  return listenable.addListener(
    this,
    listener,
    fireImmediately: fireImmediately,
    onError: onError,
    onDependencyMayHaveChanged: onDependencyMayHaveChanged,
  );
}