我正在尝试进行异步API调用,但我一直收到这个错误,我认为这是关于body_y的错误,但是我不明白它在说什么或如何正确解决它。感谢您的帮助
Future<void> _makePostRequest() async {
// set up POST request arguments
String url = 'https://myUrl';
//print(accessToken);
Map<String, String> headers = {"Authorization": "Bearer "+accessToken};
Map<String, String> body_y = {
"contactName": {
"firstName": "abc",
"middleName": "def",
"lastName": "ff",
"type": "RETAIL",
"companyName": "Company",
"tradeName": "Merchant"
}
,
"notificationPreferences": [
{
"notificationHandleType": "EMAIL",
"notificationHandle": "b@beta.yahoo.ac"
}
],
"notificationLanguage": "EN" };
// make POST request
Response response = await post(url, headers: headers, body: body_y);
// check the status code for the result
int statusCode = response.statusCode;
print(statusCode);
// this API passes back the id of the new item added to the body
String body = response.body;
}
答案 0 :(得分:0)
body
接受String
,但传递了Map<String, String
。尝试编码为json
。
import 'dart:convert`;
// Change from Map<String,String> to Map<String,dynamic>
Map<String, dynamic> body_y = {
"contactName": {
"firstName": "abc",
"middleName": "def",
"lastName": "ff",
"type": "RETAIL",
"companyName": "Company",
"tradeName": "Merchant"
},
"notificationPreferences": [
{
"notificationHandleType": "EMAIL",
"notificationHandle": "b@beta.yahoo.ac"
}
],
"notificationLanguage": "EN",
};
Response response = await post(
url,
headers: headers,
body: json.encode(body_y), // encode to json
);
var body = json.decode(response.body) as Map<String,dynamic>; // decode json to map
String id = body['id']; // if it's named 'id'