如何在后Api调用inFlutter传递这种类型的数据?



我正试图将body传递给post api,如下所示:

{
"firstname": "Demo",
"lastname": "User",
"email": "nash11@vipmail.hu",
"password": "password",
"confirm": "password",
"telephone": "1-541-754-3010",
"customer_group_id": 1,
"agree": 1,
"custom_field": {
"account": {
"1": "+364545454"
}
}
}

我的问题是如何通过这一节

"custom_field": {
"account": {
"1": "+364545454"
}
}

如果我试图传入

Map<Strng,dynamic>` data = 
{
"firstname": "Demo",
"lastname": "User",
"email": "nash11@vipmail.hu",
"password": "password",
"confirm": "password",
"telephone": "1-541-754-3010",
"customer_group_id": 1,
"agree": 1,
"custom_field": {
"account": {
"1": "+364545454"
}
}
}

显示错误

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in type cast

this may help you:

"https://opencart3-simple.api.opencart-api.com/api/rest/register" -H  "accept: application/json" -H  "X-Oc-Session: 8d63ab88c43d2ebdda1b219a98" -H  "X-Oc-Merchant-Id: 123" -H  "Content-Type: application/json" -d "{  "firstname": "Demo",  "lastname": "User",  "email": "nash111@vipmail.hu",  "password": "password",  "confirm": "password",  "telephone": "1-541-754-3010",  "customer_group_id": 1,  "agree": 1,  "custom_field": {    "account": {      "1": "+364545454"    }  }}"

对数据进行编码并遵循约定

var response = await _dio.post("$url",
options: header, data: jsonEncode(data));
Map<Strng,dynamic>` data = 
{
"firstname": "Demo",
"lastname": "User",
"email": "nash11@vipmail.hu",
"password": "password",
"confirm": "password",
"telephone": "1-541-754-3010",
"customer_group_id": 1,
"agree": 1,
"custom_field": jsonEncode({
"account": {
"1": "+364545454"
}
})
}

你可以这样传递:

var data = {}; 
data["firstname"] = "Demo";
data["lastname"] = "User";
data["email"] = "nash11@vipmail.hu";
data["password"] ="password";

并像这样传递

var response = await _dio.post($url,
data: jsonEncode(data));

相关内容

  • 没有找到相关文章

最新更新