end method

void end([
  1. R? result
])

Marks the stream as finished and closes it.

If result is provided, the result future resolves with it (the first resolution wins, mirroring pi's promise semantics). If neither result nor a completion event materializes, result completes with a StateError (pi's promise would hang forever; a hanging future is worse than a thrown error). Idempotent.

Implementation

void end([R? result]) {
  _done = true;
  if (result != null) {
    _completeResult(result);
  }
  if (!_resultCompleter.isCompleted) {
    _resultCompleter.completeError(
      StateError('EventStream ended without a completion event'),
    );
    // Suppress the unhandled-async-error report when nobody awaits
    // [result]; listeners on [result] itself still receive the error.
    _resultCompleter.future.ignore();
  }
  unawaited(_controller.close());
}