withLifecycle method
Attaches this timer to a RaiiLifecycleAware for automatic lifecycle management.
The timer will be automatically cancelled when lifecycleAware is disposed,
preventing callbacks from executing after the lifecycle has ended.
Parameters:
lifecycleAware: The parent lifecycle that will manage this timerdebugLabel: Optional label for debugging lifecycle events
Returns a RaiiTimer that wraps this timer and provides additional lifecycle management features.
Example:
// Basic usage
Timer(Duration(seconds: 3), () => print('Hello'))
.withLifecycle(this);
// With debug label
Timer.periodic(Duration(seconds: 1), (t) => print('Tick'))
.withLifecycle(this, debugLabel: 'HeartbeatTimer');
// Store reference for manual cancellation
final timer = Timer(Duration(seconds: 10), () => print('Done'))
.withLifecycle(this);
timer.cancel(); // Can cancel manually if needed
Implementation
RaiiTimer withLifecycle(
RaiiLifecycleAware lifecycleAware, {
String? debugLabel,
}) {
return RaiiTimer.withLifecycle(
lifecycleAware,
timer: this,
debugLabel: debugLabel,
);
}