无法在 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.5flutter doctor并且cleartextTraffic = "true"AndroidManifiest.xml.

哈比卜·马哈马迪

更新flutter2.0.52.2.0解决问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章