dispose method

void dispose()

Releases scheduler resources. Idempotent. Tickers that were registered with this scheduler should be disposed first; after dispose the scheduler will not invoke any callbacks.

Drains pending post-frame callbacks before clearing — a callback registered just before runtime shutdown otherwise vanishes silently, even though "after the next frame" is the contract. Exceptions in a callback are routed through Zone.current and do not abort the rest of the drain.

Implementation

void dispose() {
  if (_disposed) return;
  _disposed = true;
  _stopTimer();
  // Drain first so a callback can still run side effects (e.g. log
  // a final state) before everything is torn down. Snapshot+swap
  // matches `flushPostFrameCallbacks`'s next-frame discipline.
  if (_postFrameCallbacks.isNotEmpty) {
    final batch = _postFrameCallbacks;
    _postFrameCallbacks = <FrameCallback>[];
    final now = _clock.now;
    for (final cb in batch) {
      try {
        cb(now);
      } catch (error, stack) {
        Zone.current.handleUncaughtError(error, stack);
      }
    }
  }
  _callbacks.clear();
  _reassembleCallbacks.clear();
  _postFrameCallbacks.clear();
  _onPostFrameCallbackRegistered = null;
}