dispose method

void dispose()

Dispose owned frame pipeline state.

Implementation

void dispose() {
  if (_disposed || _disposing) {
    return;
  }
  _disposing = true;
  final failures = FirstErrorRecorder();
  // Unmount anything still pending in the inactive set (deactivated by a
  // reconciliation that ran without a following `buildScope()`, e.g. torn
  // down mid-frame) before the pipeline/render teardown below, so no
  // `State.dispose()` is lost and `Element.unmount()`'s render-object
  // detach still runs against a live `PipelineOwner`. A no-op in the
  // normal case, since `buildScope()` already finalizes every pass.
  final root = _rootRenderObject;
  try {
    failures.attempt(finalizeTree);
    if (root != null) {
      failures.attempt(() => clearRootRenderObject(root));
    }
    if (_ownsPipelineOwner) {
      failures.attempt(pipelineOwner.dispose);
    }
    failures.attempt(_pointerRouter.dispose);
    failures.attempt(focusManager.dispose);
    final globalKeyEntries = _globalKeyRegistry.entries.toList(
      growable: false,
    );
    for (final entry in globalKeyEntries) {
      failures.attempt(() => unregisterGlobalKey(entry.key, entry.value));
    }
  } finally {
    _rootRenderObject = null;
    _pointerRouter.root = null;
    _depths.clear();
    _dirtyReservations.clear();
    _pendingBuckets = _DirtyBuckets();
    _globalKeyRegistry.clear();
    _inactiveElements.clear();
    _deferredDisposals.clear();
    _tickerScheduler.setFrameCallback(null);
    _onFrame = null;
    _building = false;
    _disposed = true;
    _disposing = false;
  }

  failures.rethrowFirst();
}