errors property

Stream<Never> get errors

A broadcast stream of uncaught errors from the isolate.

When listening on the stream, errors from the isolate will be reported as errors in the stream. Be ready to handle the errors.

The stream closes when the isolate shuts down.

If the isolate shuts down while noone is listening on this stream, the stream will not be closed, and listening to the stream again after the isolate has shut down will not yield any events.

Implementation

Stream<Never> get errors {
  var controller = StreamController<Never>.broadcast(sync: true);
  controller.onListen = () {
    var port = RawReceivePort();
    port.handler = (message) {
      if (message == null) {
        // Isolate shutdown.
        port.close();
        controller.close();
      } else {
        // Uncaught error.
        final errorDescription = message[0] as String;
        final stackDescription = message[1] as String;
        var error = RemoteError(errorDescription, stackDescription);
        controller.addError(error, error.stackTrace);
      }
    };
    isolate.addErrorListener(port.sendPort);
    isolate.addOnExitListener(port.sendPort);
    controller.onCancel = () {
      port.close();
      isolate.removeErrorListener(port.sendPort);
      isolate.removeOnExitListener(port.sendPort);
    };
  };
  return controller.stream;
}