Flutter 使用 dio 遇到的问题合集

泪流满面啊,,,,,

1. postHttpLogin-异常-----DioException [bad response]: The request returned an invalid status code of 500.

/// 错误代码如下:

Future<String?> postHttpLogin(String textContent, String id)  async {
  try{
    final response = await dio.post("$baseUrl/xxxx",
        data: {'deviceId': id} ,  options: Options(responseType: ResponseType.json));
    LogsWTF("msg----------: ${response.toString()}");
    return response.toString();
  }catch(e){
    LogsWTF("msg-----------postHttpLogin-异常-----$e");
  }
}


/// 正确代码如下,改动地方 data -》 queryParameters
、
Future<String?> postHttpLogin(String textContent, String id)  async {
  try{
    final response = await dio.post("$baseUrl/xxxx",
        queryParameters: {'deviceId': id} ,  options: Options(responseType: ResponseType.json));
    LogsWTF("msg----------: ${response.toString()}");
    return response.toString();
  }catch(e){
    LogsWTF("msg-----------postHttpLogin-异常-----$e");
  }
}
2. post请求失败 DioException [bad response]: The request returned an invalid status code of 415.

这个问题有些离谱,415,415都说是请求头的问题,但结果却不是

错误代码如下:


void postHttpPaint(String textContent, String token, String aspectRatio)  async {
  try {
    ///发送 FormData:
    FormData formData =
    FormData.fromMap({
     ....数据省略
    });
    final response = await dio.post("$baseUrl/xxx", data:formData,
        options: Options(headers: {"Authorization": token,'Content-Type': 'application/json;charset=UTF-8'})
    );

    LogsWTF("---postHttpPaint----dio 数据-${response.data.toString()}---");
  } catch (e) {
    LogsWTF("post请求失败  ${e}");
  }
}

/// 正确代码如下:改动地方,将data数据直接赋值

void postHttpPaint(String textContent, String token, String aspectRatio)  async {
  try {
    final response = await dio.post("$baseUrl/xxx", data:{...数据省略...},
        options: Options(headers: {"Authorization": token,'Content-Type': 'application/json;charset=UTF-8'})
    );

    LogsWTF("---postHttpPaint----dio 数据-${response.data.toString()}---");
  } catch (e) {
    LogsWTF("post请求失败  ${e}");
  }
}


你可能感兴趣的:(flutter,前端)