![](/img/trans.png)
[英]Flutter- App working on emulator but not on device (uses http package)
[英]Flutter- dart http library ClientException
我正在使用HTTP dart 包在 flutter 中執行這樣的發布請求,但有時當網絡出現故障時,會從IOClient類拋出ClientException ,但我在 catch 塊和應用程序崩潰中沒有得到任何信息。
http.post(url, headers: headers, body: body).then((response) {
//response parsing
}).catchError((error) {
//ClientException is never catched in this block.
});
如package:http/src/io_client.dart
:
任何內部 HTTP 錯誤都應包裝為 [ClientException]。
正如文檔中提到的,這是一個明顯的案例:
潛在問題:不小心混合了同步和異步錯誤
要解決此問題,您需要將代碼包裝在 Future.sync() 中。
Future.sync() 使您的代碼能夠抵御未捕獲的異常。 如果您的函數中包含大量代碼,那么您很可能會在沒有意識到的情況下做一些危險的事情:
return new Future.sync(() {
http.post(url, headers: headers, body: body).then((response) {});
});
Future.sync() 不僅允許您處理您知道可能發生的錯誤,還可以防止錯誤從您的函數中意外泄漏。
在我的情況下,調用是使用 Microsoft Internet Information Services 作為后端的 https 地址,在 IIS 網站的 SSL 設置中,我錯誤地設置了“客戶端證書:接受”而不是“客戶端證書:忽略”,設置“忽略”解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.