android - 无法在 Flutter http 包中捕获 SocketException
问题描述
我无法捕捉SocketException
到颤振http
包。
Future<User> getUserDetail(String token) async {
try {
final response = await http.get(Uri.parse(userURL), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $token'
});
return User.fromJson(jsonDecode(response.body));
// this catch does not catch SocketException
} catch(e) {
print('Error catched: $e');
}
}
当模拟器处于飞行模式或无法访问本地服务器时,我会遇到这个问题。否则一切正常。所以我的问题是,为什么我无法捕捉到这些异常?
我的颤振版本是 2.0.5 并且没有问题,flutter doctor
我还添加了互联网权限和cleartextTraffic = "true"
.AndroidManifiest.xml
解决方案
更新颤振2.0.5
以2.2.0
解决问题。
推荐阅读
- python - 多旅行商问题蚁群优化
- python - 无法将字符串强制转换为整数 - ValueError:int() 的无效文字,基数为 10:''
- msbuild - Web 应用程序 web 包 - 禁用自动参数化
- css - tailwind css - 无法让项目填写页面,总是卡在较小的容器中
- java - 为 MaterialDatePicker 扩展 DialogFragment
- ios - 在 iOS 中更新已安装的 PWA
- http - 三位数 http 状态码之后的这些数字是什么?
- list - 在颤动中投射自定义列表的问题
- racket - 球拍中的随机黑白像素
- javascript - 如何在javascript中为此设置正则表达式?