autoDispose method

void autoDispose(
  1. void cleanup()
)

Registers a custom cleanup callback to execute when onDispose is called.

Use this to close stream subscriptions, cancel timers, abort HTTP requests, or release native resources. If the controller is already disposed, this is a no-op.

final timer = Timer.periodic(Duration(seconds: 1), (_) => tick());
autoDispose(() => timer.cancel());

Implementation

void autoDispose(void Function() cleanup) {
  if (_isDisposed) return;
  _disposers.add(cleanup);
}