尝试从 API 获取响应时 Flutter 挂起

问题描述 投票:0回答:1

我在数据库中存储了一个大约 1042 个字符的长字符串,我尝试使用 API 来检索该字符串,当我在 Postman 中运行它时,它起作用了,但是当我在 flutter 中点击请求时,应用程序突然停止并冻结了。

这是代码:

  @override
  Future<ResponseAPI> getPrivacyPolicy() async {
    try {
      Dio http = await dio();
      Response response = await http.get("/setting/type", queryParameters: {
        "type": 7,
      });

      final data = response.data;
      return ResponseAPI.fromJson(data);
    } on DioException catch (e) {
      if (e.response?.statusCode == 403) {
        return ResponseAPI(
          message: e.response?.data['message'],
          statusCode: 403,
        );
      } else {
        return const ResponseAPI(
          message: "There's something wrong, please try again later",
          statusCode: 500,
        );
      }
    } catch (e) {
      return const ResponseAPI(
        message: "There's something wrong, please try again later",
        statusCode: 500,
      );
    }
  }

这是 Postman 中的回复:

flutter dart http freeze
1个回答
0
投票

尝试为 Dio 请求选项定义responseType

  options: Options(
      method: 'GET',
      responseType: ResponseType.plain,
   ),

此外,检查运行时环境中是否抛出任何异常或使用 try 和 catch

© www.soinside.com 2019 - 2024. All rights reserved.