tryAdd method

void tryAdd(
  1. T event, {
  2. bool ignoreError = false,
  3. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Implementation

void tryAdd(
  T event, {
  bool ignoreError = false,
  void Function(Object error, StackTrace stackTrace)? onError,
}) {
  final self = this;
  if (self is StreamController<T>) {
    if (!self.isClosed) {
      if (ignoreError) {
        try {
          self.add(event);
        } catch (e, st) {
          onError?.call(e, st);
        }
      } else {
        self.add(event);
      }
    }
  } else {
    try {
      add(event);
    } on StateError {
      // Ignore because Sink has no isClosed property and throws StateError when closed.
    } catch (e, st) {
      onError?.call(e, st);
      if (!ignoreError) {
        rethrow;
      }
    }
  }
}