once function

void Function() once(
  1. void block()
)

Returns a callable that runs block only on its first invocation and is a no-op thereafter. Useful for one-time initialization guards.

Example:

final init = once(() => print('setup'));
init(); // prints 'setup'
init(); // does nothing

Implementation

void Function() once(void Function() block) {
  bool isDone = false;
  return () {
    if (!isDone) {
      isDone = true;
      block();
    }
  };
}