【问题标题】:how to send data to the internet HTTP Post request using flutter ( fixed )如何使用颤振将数据发送到互联网 HTTP Post 请求(已修复)
【发布时间】: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,试图找到一种方法来修复它但不能找不到任何

标签: json flutter post


【解决方案1】:

可能看起来很吓人,但 _InternalLinkedHashMap 只是内部 LinkedHashMap 类,它只是默认 Map 实现。

您一定是在代码中的某个地方错误地将地图对象放入了字符串类型参数中。

因为 sendForm 函数的唯一静态类型部分是函数参数本身,我认为您已将 Map 对象传递给它。 -> 也许检查你传递给该函数的内容:)

【讨论】:

  • 问题出在消息中": { ... } 它使它成为 Map 而不是 String,试图找到一种方法来修复它但找不到任何方法跨度>
  • 尝试在Map data 或Map body 上声明泛型类型,同时静态键入responseJson 对象,您可能会发现类型错误
  • 该代码现在没有错误,它可以完美运行您也可以在下面查看我的答案
【解决方案2】:

这段代码完美运行

我需要像你在代码中看到的那样将地图传递两次,然后对它进行 json 编码,

然后我必须在此处传递标题参数

{'Content-type': 'application/json'}

完整代码:

sendForm() async {
var jsonResponse;
Map data = {
  "nom": nomController.text,
  "prenom": prenomController.text,
  "tel": telController.text,
  "email": emailController.text,
  "sujet": sujetController.text,
  "message": messageController.text
};
Map body = {"message": data};
var response = await http.post(
  "https://promoteur-api.herokuapp.com/api/messages",
  body: jsonEncode(body),
  headers: {'Content-type': 'application/json'},
);
print(response.statusCode);
if (response.statusCode == 200) {
  jsonResponse = jsonEncode(response.body);
  if (jsonResponse != null) {
    print(response.body + " jsonResponse is not null");
    setState(() {
      isLoading = false;
    });
  }
} else {
  setState(() {
    isLoading = false;
  });
  print(response.body + "error jsonResponse = null");
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多