add method Null safety

  1. @override
void add(
  1. Event event
)
override

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

  • A StateError will be thrown if there is no event handler registered for the incoming event.

  • A StateError will be thrown if the bloc is closed and the event will not be processed.

Implementation

@override
void add(Event event) {
  assert(() {
    final handlerExists = _handlers.any((handler) => handler.isType(event));
    if (!handlerExists) {
      final eventType = event.runtimeType;
      throw StateError(
        '''add($eventType) was called without a registered event handler.\n'''
        '''Make sure to register a handler via on<$eventType>((event, emit) {...})''',
      );
    }
    return true;
  }());
  try {
    onEvent(event);
    _eventController.add(event);
  } catch (error, stackTrace) {
    onError(error, stackTrace);
    rethrow;
  }
}