这是我的代码:
地图
String encoded = json.encode(myMap);
// Make the POST request and wait for the response
final response = await postRequest(url: conversationUrl,
body: encoded,
headers:{'Content-Type': 'application/json'},);
但是我在主体参数中编码了一条红线,并显示错误:参数类型“String”无法分配给参数类型“Map
我已经尝试过(Dart HTTP POST with Map
所以我认为可能发生的是 json 编码方式的问题,也许
我们通常这样做
body: jsonEncode(<String, String> {
'key1':'value1', //hard coded
'key2': 'value2',
'key3': variable, //for variables given to the post function in the repository
'boolean_key': booleanVar ? "True": "False", //for boolean variables
}
)
此外,json 编码的文档是这样说的:
Shorthand for json.encode. Useful if a local variable shadows the global json constant.
Example:
const data = {'text': 'foo', 'value': 2, 'status': false, 'extra': null};
final String jsonString = jsonEncode(data);
print(jsonString); // {"text":"foo","value":2,"status":false,"extra":null}
尝试将字符串编码为final
类型