【发布时间】:2021-05-01 11:16:57
【问题描述】:
已修复,请检查下面我自己的答案中的代码
亲爱的 Stackoverflow 社区,您好,我是 Flutter 的新手,我无法真正理解我在尝试从 Flutter 应用程序发送帖子请求时遇到的这个问题
_CastError (type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast)
这是我的代码
sendForm() async {
var jsonResponse;
// the error line is below, in the http method
var response = await http.post(
"https://promoteur-api.herokuapp.com/api/messages",
body: {
// update
// the problem is in the message":{...} it makes it Map<String, String> instead of String
"message": {
"nom": nomController.text,
"prenom": prenomController.text,
"tel": telController.text,
"email": emailController.text,
"sujet": sujetController.text,
"message": messageController.text
}
},
);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if (jsonResponse != null) {
setState(() {
isLoading = false;
});
}
} else {
setState(() {
isLoading = false;
});
print(response.body);
}
}
我在这里调用方法
onPressed: () {
sendForm();
},
这是邮递员中的请求:
{
"message": {
"nom": "Testing name",
"prenom": "Testing name",
"tel": "25252525",
"email": "testing@gmail.com",
"sujet": "Testing subject",
"message": "something here"
}
}
和回应:
{
"id": 6,
"nom": "Testing name",
"prenom": "Testing name",
"tel": 25252525,
"email": "testing@gmail.com",
"sujet": "Testing subject",
"message": "something here"
}
【问题讨论】:
-
我认为问题出在 print(response.body);而是尝试 print(response['body']);
-
不,这绝对不是它的原因,问题出在
"message": { ... }行中,它使它成为 Map而不是 String,试图找到一种方法来修复它但不能找不到任何