【问题标题】:How to make a POST request in a flutter for web API如何在 web API 的颤动中发出 POST 请求
【发布时间】:2018-12-24 01:22:51
【问题描述】:

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

【问题讨论】:

  • 哪行代码导致错误?你能从堆栈跟踪中提供更大的部分吗?请添加flutter doctor的输出
  • 未处理的异常:E/flutter (24453): type '_InternalLinkedHashMap' 不是类型 'Map' E/flutter (24453): #0 NetworkUtil.requestMethod (package:fitnessstudio/globals.dart:76:61) E/flutter (24453):
  • 这是错误它的响应初始化错误或http post错误
  • package:fitnessstudio/globals.dart:76:61 的代码行是什么?
  • 这是我赢得的代码写在这里我解决了这个问题非常感谢你的重播

标签: http flutter


【解决方案1】:

我用下面的代码解决了这个问题

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

【讨论】:

  • 我现在得到动态响应如何将动态转换为对象?前任。我想将动态转换为 List 怎么做?
  • 像魅力一样工作!谢谢
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2019-10-15
相关资源
最近更新 更多