W3cubDocs

/Dart 2

Stream<T>.error constructor

  1. @Since("2.5")
Stream<T>.error(Object error, [ StackTrace stackTrace ])

Creates a stream which emits a single error event before completing.

This stream emits a single error event of error and stackTrace and then completes with a done event.

Example:

Future<void> tryThings(Stream<int> data) async {
  try {
    await for (var x in data) {
      print("Data: $x");
    }
  } catch (e) {
    print(e);
  }
}
tryThings(Stream<int>.error("Error")); // prints "Error".

The returned stream is effectively equivalent to one created by Future<T>.error(error, stackTrace).asStream(), by or (() async* { throw error; } ()), except that you can control the stack trace as well.

Implementation

@Since("2.5")
factory Stream.error(Object error, [StackTrace stackTrace]) =>
    (_AsyncStreamController<T>(null, null, null, null)
          .._addError(error, stackTrace)
          .._closeUnchecked())
        .stream;

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-async/Stream/Stream.error.html