listen<Result> method
ProviderSubscription<Result>
listen<Result>(
- ProviderListenable<
Result> provider, { - void mayHaveChanged(
- ProviderSubscription<
Result> sub
- ProviderSubscription<
- void didChange(
- ProviderSubscription<
Result> sub
- ProviderSubscription<
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:
- ProviderSubscription, which allows reading the current value and closing the subscription.
- ProviderReference.watch, which is an easier way for providers to listen to another provider.
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');
}
}