cancel method

void cancel()

Cancels the timer and disposes its lifecycle.

This method safely cancels the timer and performs proper lifecycle cleanup:

  • Checks if the timer is still active and lifecycle is mounted
  • Cancels the underlying timer
  • Unregisters from the parent lifecycle
  • Performs lifecycle cleanup

After calling cancel, the timer will not fire and isActive will return false.

This method is safe to call multiple times or after the timer has already completed. It only performs disposal if the timer is both active and the lifecycle is still mounted.

Example:

final timer = Timer(Duration(seconds: 10), () => print('Hello'))
    .withLifecycle(this);

// User cancelled the operation
timer.cancel();

print(timer.isActive); // false

// Safe to call again - no-op
timer.cancel();

Implementation

void cancel() {
  if (isActive && isLifecycleMounted()) {
    _lifecycleAware.unregisterLifecycle(this);
    disposeLifecycle();
  }
}