setError method Null safety

void setError(
  1. Object error,
  2. [StackTrace? stackTrace]
)

Indicates that there was an error connecting the channel.

This makes the stream emit error and close. It makes the sink discard all its events.

Either setChannel or setError may be called at most once. Trying to call either of them again will fail.

Implementation

void setError(Object error, [StackTrace? stackTrace]) {
  if (_set) throw StateError('The channel has already been set.');
  _set = true;

  _streamCompleter.setError(error, stackTrace);
  _sinkCompleter.setDestinationSink(NullStreamSink());
}