add method

void add(
  1. Event event
)

Notifies the Bloc of a new event which triggers all corresponding handlers.

  • An AssertionError will be thrown if there is no event handler registered for the incoming event.

Implementation

void add(Event event) {
  final index = _handlers.indexWhere((e) => e.isType(event));
  final eventType = event.runtimeType;
  assert(
    index != -1,
    'on<$eventType>(...) must be called before add($eventType)',
  );

  final fn = _handlers[index].fn;
  final emitter = Emitter<State>(
    (newState) => state = newState,
  );

  final result = fn(event, emitter) as FutureOr<void>;

  /// disable the emitter after handler has been completed
  if (result is Future) {
    result.then(
      (_) => emitter._disable(),
    );
  } else {
    emitter._disable();
  }
}