dispose method
Release the resources associated to this ProviderElementBase.
This will be invoked when:
- the provider is using
autoDispose
and it is no-longer used. - the associated ProviderContainer is disposed
On the other hand, this life-cycle will not be executed when a provider rebuilds.
As opposed to runOnDispose, this life-cycle is executed only for the lifetime of this element.
Implementation
@override
void dispose() {
final completer = _futureCompleter;
if (completer != null) {
// Whatever happens after this, the error is emitted post dispose of the provider.
// So the error doesn't matter anymore.
completer.future.ignore();
final lastFuture = _lastFuture;
if (lastFuture != null) {
// The completer will be completed by the while loop in handleStream
final cancelSubscription = _cancelSubscription;
if (cancelSubscription != null) {
completer.future
.then(
(_) {},
// ignore: avoid_types_on_closure_parameters
onError: (Object _) {},
)
.whenComplete(cancelSubscription);
}
// Prevent super.dispose from cancelling the subscription on the "last"
// stream value, so that it can be sent to `provider.future`.
_lastFuture = null;
_lastFutureSub = null;
_cancelSubscription = null;
} else {
// The listened stream completed during a "loading" state.
completer.completeError(
_missingLastValueError(),
StackTrace.current,
);
}
}
super.dispose();
}