dispose method

  1. @override
Future<Null> dispose()
inherited

Dispose of the object, cleaning up to prevent memory leaks.

Implementation

@override
Future<Null> dispose() async {
  Stopwatch? stopwatch;
  if (_debugModeTelemetry) {
    stopwatch = Stopwatch()..start();
  }

  _logDispose();

  if (isDisposed) {
    return null;
  }
  if (isOrWillBeDisposed) {
    return didDispose;
  }

  _state = DisposableState.awaitingDisposal;

  await onWillDispose();

  while (_awaitableFutures.isNotEmpty) {
    final futures = _awaitableFutures.toList();
    _awaitableFutures.clear();
    await Future.wait(futures);
  }

  _state = DisposableState.disposing;

  for (var disposable in _internalDisposables) {
    await disposable.dispose();
  }
  _internalDisposables.clear();

  await onDispose();

  _didDispose.complete();
  _state = DisposableState.disposed;
  if (_debugModeLogging) {
    _logger!.info('Disposed object $hashCode of type $runtimeType');
  }

  if (_debugModeTelemetry) {
    stopwatch!.stop();
    var t = stopwatch.elapsedMicroseconds / 1000000.0;
    _logger!.info('$runtimeType $hashCode took $t seconds to dispose');
  }

  flagLeak();
}