errors 属性

Stream errors

返回一个从独立进程中未捕获错误的广播流。

每个错误都作为流中的错误事件提供。

实际的错误对象和 stackTraces 可能不一定是与实际独立进程相同的对象类型,但它们总是具有相同的 Object.toString 结果。

此流基于 addErrorListenerremoveErrorListener

实现

Stream get errors {
  StreamController controller = StreamController.broadcast(sync: true);
  RawReceivePort? port;
  void handleError(Object? message) {
    var listMessage = message as List<Object?>;
    var errorDescription = listMessage[0] as String;
    var stackDescription = listMessage[1] as String;
    var error = RemoteError(errorDescription, stackDescription);
    controller.addError(error, error.stackTrace);
  }

  controller.onListen = () {
    RawReceivePort receivePort = RawReceivePort(handleError);
    port = receivePort;
    this.addErrorListener(receivePort.sendPort);
  };
  controller.onCancel = () {
    var listenPort = port!;
    port = null;
    this.removeErrorListener(listenPort.sendPort);
    listenPort.close();
  };
  return controller.stream;
}