dart net::ERR_TIMED_OUTFlutterWeb http程序包

z9ju0rcb  于 2023-02-10  发布在  Flutter
关注(0)|答案(1)|浏览(115)

我使用HTTP包向服务器发送请求并处理错误,但在向服务器发送请求时出现net::ERR_TIMED_OUT错误,我不知道如何处理该错误。

Future<Session> login(String username, String password) async {
    final request = Uri.https(
      _baseUrlGeocoding,
      '/login',
      {'email': username.trim(), 'password': password},
    );

    try {
      final response = await _httpClient.post(request);
      if (response.statusCode != 200) {
        throw LoginRequestServerFailure();
      }

      final responseJson = jsonDecode(response.body) as Map<String, dynamic>;

      if (responseJson['message'] != 'OK') throw LoginCredentialsFailure();

      return Session.fromJson(responseJson['data'] as Map<String, dynamic>);
    } catch (error) {
      print(error.toString());
      throw InternetConnectionFailure();
    }
  }

在发送请求而不是捕捉错误后,调试器在发送请求点冻结,没有抛出错误。在Web控制台中,我收到了以下错误消息:

POST https://sample.com/account?email=g81%40ekcsoft.com&password=123456 net::ERR_TIMED_OUT

当向服务器发送请求时,如何处理flutter http包中的net::ERR_TIMED_OUT错误?

3ks5zfa0

3ks5zfa01#

这样写

Uri myUri = Uri.parse(url);
      var response = await http.post(
        myUri,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer $token',
        },
      ).timeout(const Duration(minutes: 3));

相关问题