泪流满面啊,,,,,文章来源:https://www.toymoban.com/news/detail-775754.html
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都说是请求头的问题,但结果却不是文章来源地址https://www.toymoban.com/news/detail-775754.html
错误代码如下:
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 遇到的问题合集的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!