once<T> method

Future<T> once<T>(
  1. String? type, [
  2. EventCallback<T>? callback
])
inherited

Same as on but with a callback that is only called once.

  EventEmitter events = EventEmitter();
    ...
  events.once('message', (String data) => print('String: $data'));

Can be used asynchronously, such as:

  EventEmitter events = EventEmitter();
  final dynamicValue = await events.once('message');
  final stringValue = await events.once<String>('message');

Implementation

Future<T> once<T>(String? type, [EventCallback<T>? callback]) {
  final completer = Completer<T>();
  final listener = EventListener<T>(
    type,
    (data) {
      callback?.call(data);
      completer.complete(data);
    },
    once: true,
  );
  addEventListener(listener);
  return completer.future;
}