RaiiTimer.withLifecycle constructor

RaiiTimer.withLifecycle(
  1. RaiiLifecycleAware lifecycleAware, {
  2. required Timer timer,
  3. String? debugLabel,
})

Creates a new RaiiTimer and attaches it to the given lifecycleAware.

The timer is automatically registered with lifecycleAware and will be cancelled when lifecycleAware is disposed.

Parameters:

  • lifecycleAware: The parent lifecycle that will manage this timer
  • timer: The underlying Timer instance to manage
  • debugLabel: Optional label for debugging lifecycle events

Example:

// Typically created through the extension method:
final timer = Timer(Duration(seconds: 5), () => print('Done!'))
    .withLifecycle(this, debugLabel: 'MyTimer');

// But can be created directly:
final raiiTimer = RaiiTimer.withLifecycle(
  myComponent,
  timer: Timer(Duration(seconds: 5), () => print('Done!')),
  debugLabel: 'MyTimer',
);

Note: In most cases, prefer using TimerRaiiExt.withLifecycle extension method instead of creating RaiiTimer instances directly.

Implementation

RaiiTimer.withLifecycle(
  RaiiLifecycleAware lifecycleAware, {
  required Timer timer,
  this.debugLabel,
})  : _lifecycleAware = lifecycleAware,
      _timer = timer {
  _lifecycleAware.registerLifecycle(this);
}