【问题标题】:type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'String' in type cast类型“_InternalLinkedHashMap<dynamic, dynamic>”不是类型转换中“String”类型的子类型
【发布时间】:2019-08-28 10:40:08
【问题描述】:

我正在尝试将嵌套的 JSON 对象发布到 API。这是我正在尝试的简单代码。

Map m = {
   "email": 's@a.com',
   "password": "123",
   "billing" : {
      "first_name": "Samarth",
      "last_name": "Agarwal",
   }
};

final response = await http.post(url, body: m, headers: {
   "Content-Type": "application/json",
   "Accept": "application/json"
});

我收到错误消息: type '_InternalLinkedHashMap&lt;dynamic, dynamic&gt;' is not a subtype of type 'String' in type cast。如果我没有传入 billing 对象,它本身就是另一个映射,那么请求成功完成。

如何在 dart 中使用 HTTP 成功地将嵌套的 JSON 对象发送到 API?

【问题讨论】:

    标签: json http post dart flutter


    【解决方案1】:

    您需要使用 json.encode(m) 对其进行 json 编码,然后将其作为正文发送。

    【讨论】:

    • 这会将 json 转换为字符串 avd api 不接受字符串。我试过了。
    猜你喜欢
    • 2020-03-03
    • 2021-04-03
    • 2021-10-23
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2019-03-14
    • 2021-06-17
    • 2021-01-30
    相关资源
    最近更新 更多