DateTimeLoopController class

A controller that emits DateTime updates at intervals specified by a TimeUnit.

The DateTimeLoopController provides a stream of DateTime objects that can be listened to by multiple subscribers. The stream emits updates based on the system's current time, with the frequency determined by the timeUnit parameter.

Key Features:

  • Emits DateTime updates at intervals specified by timeUnit.
  • Optionally emits an initial DateTime when the first listener subscribes.
  • Uses a broadcast stream to support multiple listeners.
  • Automatically starts emitting updates when the first listener subscribes and stops when the last listener unsubscribes.
  • Supports manual pausing and resuming of updates for resource management (e.g., when the app is backgrounded).

Example Usage:

final controller = DateTimeLoopController(timeUnit: TimeUnit.seconds);
controller.dateTimeStream.listen((dateTime) {
  print('Current time: $dateTime');
});
// Pause updates
controller.pause();
// Resume updates with an immediate trigger
controller.resume(triggerImmediate: true);
// Dispose of the controller when no longer needed
controller.dispose();

Important: Call dispose when the controller is no longer needed to prevent memory leaks.

Constructors

DateTimeLoopController({required TimeUnit timeUnit, bool triggerOnStart = true, DateTime getNow() = DateTime.now})
Creates a DateTimeLoopController with the specified timeUnit and triggerOnStart.

Properties

dateTimeStream Stream<DateTime>
The broadcast stream that emits DateTime updates at intervals specified by timeUnit. Multiple listeners can subscribe to this stream.
no setter
getNow DateTime Function()
A function that returns the current DateTime. Defaults to DateTime.now. This can be overridden for testing or simulation purposes to mock the system time.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeUnit TimeUnit
The time unit that determines the interval at which DateTime updates are emitted. For example, TimeUnit.seconds emits updates every second.
final
triggerOnStart bool
Whether to emit an initial DateTime when the first listener subscribes. If true, the stream emits the current DateTime immediately upon subscription. If false, the first emission occurs at the next interval boundary. Defaults to true.
final

Methods

dispose() → void
Closes the stream controller, cancels the timer, and stops emitting DateTime updates.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pause() → void
Pauses the emission of DateTime updates, canceling the internal timer. This is useful for saving resources when updates are not needed, such as when the app is backgrounded.
resume({bool triggerImmediate = true}) → void
Resumes the emission of DateTime updates if there are active listeners. Optionally triggers an immediate update.
toString() String
A string representation of this object.
inherited
triggerNow() → void
Emits the current DateTime immediately to the stream.

Operators

operator ==(Object other) bool
The equality operator.
inherited