listen<State> method Null safety

ProviderSubscription<State> listen<State>(
  1. ProviderListenable<State> provider,
  2. void listener(
    1. State? previous,
    2. State next
    ),
  3. {bool fireImmediately = false,
  4. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?}
)

Subscribe to this provider.

See also:

  • ProviderSubscription, which allows reading the current value and closing the subscription.
  • Ref.watch, which is an easier way for providers to listen to another provider.

Implementation

ProviderSubscription<State> listen<State>(
  ProviderListenable<State> provider,
  void Function(State? previous, State next) listener, {
  bool fireImmediately = false,
  void Function(Object error, StackTrace stackTrace)? onError,
}) {
  // TODO test always flushed provider
  if (provider is _ProviderSelector<Object?, State>) {
    return provider.listen(
      this,
      listener,
      fireImmediately: fireImmediately,
      onError: onError,
    );
  }

  final element = readProviderElement(provider as ProviderBase<State>);

  return element.addListener(
    provider,
    listener,
    fireImmediately: fireImmediately,
    onError: onError,
  );
}