gpt4 book ai didi

http - 如何在 web API 的 flutter 中发出 POST 请求

转载 作者:IT老高 更新时间:2023-10-28 12:44:36 26 4
gpt4 key购买 nike

我尝试编写一个常见的 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>

最佳答案

我通过以下代码解决了这个问题

     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);
});
}

关于http - 如何在 web API 的 flutter 中发出 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51362777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com