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

标签 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',
  };

  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/

相关文章:

android - Flutter app后台使用compute函数

database - 用于构建可扩展 Web 应用程序的书籍? (数据库性能/调优、网络、一般性能等)

php - 如何在一个 HTTP 请求中提供多种字体?

android - Flutter youtube_player_flutter : ^7. 0.0+6 全屏

flutter - 如何使下拉按钮创建的列表形状呈圆形?

android - 如何在 Flutter 中自动刷新 listView?

http - Flutter consolidateHttpClientResponseBytes未定义错误?

java - Java发送HTTP请求时连接超时

php - img http 引用

java - 无法通过代理连接到 websocket