我尝试编写一个常见的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',
};
var response =await http.post(url, body: body, headers: headers);
final responseJson = json.decode(response.body);
print(responseJson);
return response;
}
错误
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>
答案 0 :(得分:4)
我通过使用以下代码解决了问题
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);
});
}