listen<Result> method

ProviderSubscription<Result> listen<Result>(
  1. ProviderListenable<Result> provider,
  2. {void mayHaveChanged(
    1. ProviderSubscription<Result> sub
    )?,
  3. void didChange(
    1. ProviderSubscription<Result> sub
    )?}
)

Subscribe to this provider.

  • mayHaveChanged will be called the first time that any of the dependencies of a provider changed.

  • didChange will be called after the first ProviderSubscription.flush or ProviderSubscription.read, only when it is confirmed that the value exposed has changed.

See also:

Implementation

ProviderSubscription<Result> listen<Result>(
  ProviderListenable<Result> provider, {
  void Function(ProviderSubscription<Result> sub)? mayHaveChanged,
  void Function(ProviderSubscription<Result> sub)? didChange,
}) {
  if (provider is ProviderBase<Object?, Result>) {
    return readProviderElement(provider).listen(
      mayHaveChanged: mayHaveChanged,
      didChange: didChange,
    );
  } else if (provider is ProviderSelector<Object?, Result>) {
    return provider._listen(
      this,
      mayHaveChanged: mayHaveChanged,
      didChange: didChange,
    );
  } else {
    throw UnsupportedError('Unknown ProviderListenable $provider');
  }
}