我试图将嵌套的JSON对象发布到API。这是我正在尝试的简单代码。
Map m = {
"email": '[email protected]',
"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<dynamic, dynamic>' is not a subtype of type 'String' in type cast
。如果我没有传入billing
对象,这是另一个映射本身,那么请求成功完成。
如何使用dart中的HTTP成功将嵌套的JSON对象发送到API?
你需要用json.encode(m)
对它进行json编码,然后将其作为正文发送。