Exception 如何捕捉颤振中异步函数的异常?
我使用许多API与服务器通信,并希望处理来自此调用的意外异常。但是,它根本无法捕获异常。如何处理Exception 如何捕捉颤振中异步函数的异常?,exception,dart,flutter,Exception,Dart,Flutter,我使用许多API与服务器通信,并希望处理来自此调用的意外异常。但是,它根本无法捕获异常。如何处理 if(event is RandomChatEventMatchStart){ yield RandomChatState.loading(); try { chatRoomID = await _api.getRoomID(); if(chatRoomID.isEmpty){ chatRoomID = await _api.makeChatRoom();
if(event is RandomChatEventMatchStart){
yield RandomChatState.loading();
try {
chatRoomID = await _api.getRoomID();
if(chatRoomID.isEmpty){
chatRoomID = await _api.makeChatRoom();
yield RandomChatState.madeChatRoom(chatRoomID);
} else {
await _api.enterChatRoom(chatRoomID);
yield RandomChatState.matchSucceeded();
}
} catch(exception) {
yield RandomChatState.apiFailed(exception.toString());
}
}
E/flatter(12376):PlatformException(错误,无效文档引用。文档引用必须有偶数个段,但消息有1个空段)E/flatter(12376):#0 StandardMethodCodec.decodeEnvelope包:flatter/../services/message_codecs。dart:564 E/flatter(12376):
#1 MethodChannel.invokeMethod包:flatter/../services/platform\u channel.dart:292 E/flatter(12376):E/flatter(12376):\2 DocumentReference.snapshots…\\src\文件参考。dart:126 E/颤振(12376):\3
_Runguard(dart:async/stream\u controller.dart:805:24)E/Flatter(12376):#4(BroadcastStreamController.)订阅(dart:async/broadcast\u stream\u controller.dart:213:7)E/Flatter(12376):
#5 _ControllerStream._createSubscription(dart:async/stream_controller.dart:818:19)E/flift(12376):#6
_StreamImpl.listen(dart:async/stream\u impl.dart:472:9)E/flatter(12376):#7\u StreamBuilderBaseState.\u订阅包:flatter/../widgets/async.dart:135 E/flatter(12376):#8
_StreamBuilderBaseState.didUpdateWidget包:flatter/../widgets/async.dart:120 E/flatter(12376):#9 stateflement.update包:flatter/../widgets/framework.dart:3863 E/flatter(12376):#10 Element.updateChild
如果您使用的是async
/wait
,那么您可以使用try
/catch
@GünterZöchbauer首先,我是这样想的,但它无法捕获异常!导致此异常的代码行。它似乎来自StreamBuilder
,而不是您发布的代码。与此代码相关
E/flutter (12376): PlatformException(error, Invalid document reference. Document references must have an even number of segments, but messages has 1, null) E/flutter (12376): #0 StandardMethodCodec.decodeEnvelope package:flutter/…/services/message_codecs.dart:564 E/flutter (12376):
#1 MethodChannel.invokeMethod package:flutter/…/services/platform_channel.dart:292 E/flutter (12376): <asynchronous suspension> E/flutter (12376): #2 DocumentReference.snapshots.<anonymous closure> ..\…\src\document_reference.dart:126 E/flutter (12376): #3
_runGuarded (dart:async/stream_controller.dart:805:24) E/flutter (12376): #4 _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:213:7) E/flutter (12376):
#5 _ControllerStream._createSubscription (dart:async/stream_controller.dart:818:19) E/flutter (12376): #6
_StreamImpl.listen (dart:async/stream_impl.dart:472:9) E/flutter (12376): #7 _StreamBuilderBaseState._subscribe package:flutter/…/widgets/async.dart:135 E/flutter (12376): #8
_StreamBuilderBaseState.didUpdateWidget package:flutter/…/widgets/async.dart:120 E/flutter (12376): #9 StatefulElement.update package:flutter/…/widgets/framework.dart:3863 E/flutter (12376): #10 Element.updateChild