withFallback<T> function

Stream<T> withFallback<T>(
  1. Stream<T> stream,
  2. T fallbackElement
)

Returns a stream that emits the events from the given stream, or the fallback element if the stream is empty.

Implementation

Stream<T> withFallback<T>(Stream<T> stream, T fallbackElement) async* {
  var isEmpty = true;
  await for (final event in stream) {
    isEmpty = false;
    yield event;
  }
  if (isEmpty) {
    yield fallbackElement;
  }
}