Http 如何在web API的颤振中发出POST请求
我尝试编写一个常见的POST请求方法,但它的错误如下Http 如何在web API的颤振中发出POST请求,http,flutter,Http,Flutter,我尝试编写一个常见的POST请求方法,但它的错误如下 Future<dynamic> requestMethod() async { var url = "https://testurl"; var body = json.encode({"program_id":"1211"}); Map headers = { 'Content-type' : 'application/json', 'Accept': 'application/json', }
Future<dynamic> requestMethod() async {
var url = "https://testurl";
var body = json.encode({"program_id":"1211"});
Map headers = {
'Content-type' : 'application/json',
'Accept': 'application/json',
};
var response =await http.post(url, body: body, headers: headers);
final responseJson = json.decode(response.body);
print(responseJson);
return response;
}
Future requestMethod()异步{
变量url=”https://testurl";
var body=json.encode({“program_id”:“1211”});
映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
var response=wait http.post(url,body:body,headers:headers);
final responseJson=json.decode(response.body);
印刷品(responseJson);
返回响应;
}
错误
Unhandled exception: E/flutter (24453): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>' E/flutter (24453): #0 NetworkUtil.requestMethod (package:fitnessstudio/globals.dart:76:61) E/flutter (24453): <asynchronous suspension>
未处理的异常:E/flatter(24453):类型“\u InternalLinkedHashMap”不是类型“Map”的子类型E/flatter(24453):\0 NetworkUtil.requestMethod(包:fitnessstudio/globals.dart:76:61)E/flatter(24453):
我使用以下代码解决了问题
Future<dynamic> post(String url,var body)async{
return await http
.post(Uri.encodeFull(url), body: body, headers: {"Accept":"application/json"})
.then((http.Response response) {
// print(response.body);
final int statusCode = response.statusCode;
if (statusCode < 200 || statusCode > 400 || json == null) {
throw new Exception("Error while fetching data");
}
return _decoder.convert(response.body);
});
}
Future post(字符串url,变量体)异步{
返回等待http
.post(Uri.encodeFull(url),主体:主体,标题:{“接受”:“应用程序/json”})
.然后((http.Response){
//打印(响应.正文);
最终int statusCode=response.statusCode;
if(statusCode<200 | | statusCode>400 | | json==null){
抛出新异常(“获取数据时出错”);
}
return _decoder.convert(response.body);
});
}
哪一行代码导致错误?你能从堆栈跟踪中提供更大的部分吗?请添加flatter doctor
未处理异常的输出:E/flatter(24453):类型“\u InternalLinkedHashMap”不是类型“Map”的子类型E/flatter(24453):#0 NetworkUtil.requestMethod(包:fitnessstudio/globals.dart:76:61)E/flatter(24453):这是错误它的响应初始化错误或http post错误包中的代码行是什么:fitnessstudio/globals。dart:76:61?这是我的源代码在这里编写我已解决问题非常感谢您的重播我现在得到的响应是动态的如何将动态转换为对象?我想将dynamic转换为List如何做到这一点?效果很好!谢谢