TimerRaiiExt extension
Extension on Timer that adds RAII lifecycle management capabilities.
This extension allows you to attach any Timer to a RaiiLifecycleAware object, ensuring automatic cancellation when the lifecycle is disposed.
Example:
class MyComponent with RaiiLifecycleMixin {
void setupTimers() {
// One-time timer
Timer(Duration(seconds: 5), () {
print('Executed after 5 seconds');
}).withLifecycle(this);
// Periodic timer
Timer.periodic(Duration(seconds: 1), (timer) {
print('Tick: ${timer.tick}');
}).withLifecycle(this, debugLabel: 'PeriodicTick');
// All timers are automatically cancelled when disposeLifecycle() is called
}
}
- on
Methods
-
withLifecycle(
RaiiLifecycleAware lifecycleAware, {String? debugLabel}) → RaiiTimer -
Available on Timer, provided by the TimerRaiiExt extension
Attaches this timer to a RaiiLifecycleAware for automatic lifecycle management.