Android 颤振-dart http库客户端异常
我在Flatter中使用HTTP dart包执行类似的post请求,但有时当网络出现故障时,IOClient类会抛出客户端异常,但我在catch block中没有得到任何结果,应用程序崩溃Android 颤振-dart http库客户端异常,android,dart,flutter,Android,Dart,Flutter,我在Flatter中使用HTTP dart包执行类似的post请求,但有时当网络出现故障时,IOClient类会抛出客户端异常,但我在catch block中没有得到任何结果,应用程序崩溃 http.post(url, headers: headers, body: body).then((response) { //response parsing }).catchError((error) { //ClientException is never catched in this blo
http.post(url, headers: headers, body: body).then((response) {
//response parsing
}).catchError((error) {
//ClientException is never catched in this block.
});
如
包中所述:http/src/io_client.dart
:
任何内部HTTP错误都应包装为[ClientException]s
如本节所述,这是一个明显的例子:
潜在问题:意外地混合了同步和异步错误
要解决此问题,您需要在Future.sync()中包装代码
Future.sync()使您的代码能够抵御未捕获的异常。如果您的函数中包含大量代码,则很有可能在未意识到的情况下执行了一些危险的操作:
Future.sync()不仅允许您处理已知可能发生的错误,还可以防止错误意外泄漏到函数中
在我的例子中,调用的是使用Microsoft Internet Information Services作为后端的https地址,在IIS网站的SSL设置中,我错误地设置了“客户端证书:接受”而不是“客户端证书:忽略”,设置“忽略”解决了问题。是否尝试将其包装在
try{…}catch(e){
?我正在使用Future API not async await,我认为在Future API中未使用try catch?尝试它不会太难。如果抛出同步错误,则try/catch仍然有效。您还可以尝试onError
,如try catch with await中所示,它似乎正在工作。到现在还不能繁殖。
return new Future.sync(() {
http.post(url, headers: headers, body: body).then((response) {});
});