onDispose method

  1. @override
Future<Null> onDispose()

Callback to allow arbitrary cleanup on dispose.

Implementation

@override
Future<Null> onDispose() async {
  Completer<Null>? completer = Completer<Null>();
  final completerFuture = completer.future;

  // Set up an onError handler in case onMaybeUnmounted isn't called due to
  // an error, and an async error is thrown instead.
  runZonedGuarded(() {
    // Attempt to unmount the content safely
    _safeUnmountContent(force: true, onMaybeUnmounted: (_) {
      completer?.complete();
      // Clear out to not retain it in the onError closure, which has
      // an indefinitely long lifetime.
      completer = null;
    });
  }, (error, stackTrace) {
    completer?.completeError(error, stackTrace);
    // Clear out to not retain it in the onError closure, which has
    // an indefinitely long lifetime.
    completer = null;
  });

  await completerFuture;

  // Prevent any closures retaining `_selfRef` from retaining `this`.
  _selfRef.current = null;

  await super.onDispose();
}