tick static method

Cmd tick(
  1. Duration duration,
  2. Msg? callback(
    1. DateTime time
    )
)

A command that sends a message after a delay.

The callback receives the current time and should return the message to send. Returns null to send no message.

// Send a TickMsg after 1 second
Cmd.tick(Duration(seconds: 1), (time) => TickMsg(time));

// Chain ticks for repeated behavior
@override
(Model, Cmd?) update(Msg msg) {
  return switch (msg) {
    TickMsg() => (
      decrementCounter(),
      Cmd.tick(Duration(seconds: 1), (t) => TickMsg(t)),
    ),
    _ => (this, null),
  };
}

Implementation

static Cmd tick(Duration duration, Msg? Function(DateTime time) callback) {
  return Cmd(() async {
    await Future<void>.delayed(duration);
    return callback(DateTime.now());
  });
}