本文主要是介绍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 使用 dio 遇到的问题合集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!