首页 > 解决方案 > 无法在 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

标签: androidflutterhttpdart

解决方案


更新颤振2.0.52.2.0解决问题。


推荐阅读