launchWhenLifecycleStateDestroyed<T> method
Future<T>
launchWhenLifecycleStateDestroyed<
T>({ - bool runWithDelayed = false,
- Cancellable? cancellable,
- required FutureOr<T> block(
- Cancellable cancellable
),
})
Implementation
Future<T> launchWhenLifecycleStateDestroyed<T>(
{bool runWithDelayed = false,
Cancellable? cancellable,
required FutureOr<T> Function(Cancellable cancellable) block}) {
Completer<T> completer = runWithDelayed ? Completer() : Completer.sync();
final observer = LifecycleObserver.stateChange((state) {
if (state == LifecycleState.destroyed &&
!completer.isCompleted &&
cancellable?.isUnavailable != true) {
if (runWithDelayed) {
completer.complete(Future(() => block(cancellable ?? Cancellable())));
} else {
completer.complete(block(cancellable ?? Cancellable()));
}
}
});
addLifecycleObserver(observer);
cancellable?.onCancel
.then((_) => removeLifecycleObserver(observer, fullCycle: false));
return completer.future;
}