errors 属性
返回从隔离器中未捕获错误的广播流。
每个错误都作为流上的错误事件提供。
实际的错误对象和 stackTraces 不一定是实际隔离器中相同类型的对象,但它们的 Object.toString 结果将始终相同。
此流基于 addErrorListener 和 removeErrorListener。
实现
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;
}