简体   繁体   English

如何将数据发送到互联网 HTTP 使用 flutter 发布请求(已修复)

[英]how to send data to the internet HTTP Post request using flutter ( fixed )

FIXED, CHECK THE CODE IN MY OWN ANSWER BELOW已修复,请检查下面我自己的答案中的代码

Hello dear Stackoverflow community, i'm kinda new to flutter and i can't really understand this problem i'm getting while trying to send a post request from the flutter app你好,亲爱的 Stackoverflow 社区,我对 flutter 有点陌生,我无法真正理解我在尝试从 flutter 应用程序发送帖子请求时遇到的这个问题

_CastError (type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast)

here is my code这是我的代码

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);
  }
}

and i'm calling the method here我在这里调用方法

 onPressed: () {
   sendForm();
 },

here is the request in postman:这是 postman 中的请求:

{
   "message": {
     "nom": "Testing name",
     "prenom": "Testing name",
     "tel": "25252525",
     "email": "testing@gmail.com",
     "sujet": "Testing subject",
     "message": "something here"
   }
}

and the response:和回应:

{
"id": 6,
"nom": "Testing name",
"prenom": "Testing name",
"tel": 25252525,
"email": "testing@gmail.com",
"sujet": "Testing subject",
"message": "something here"
}

Might look frightening but _InternalLinkedHashMap is just the internal LinkedHashMap class which is just the default Map implementation.可能看起来很可怕,但 _InternalLinkedHashMap 只是内部 LinkedHashMap class ,它只是默认的 Map 实现。

Somewhere in your code you must have wrongly placed a map object into a string type parameter.在您的代码中的某处,您必须错误地将 map object 放入字符串类型参数中。

Because the only statically typed part of your sendForm function is the function parameters itself I think you have passed a Map object to it. Because the only statically typed part of your sendForm function is the function parameters itself I think you have passed a Map object to it. -> maybe check what you passed to that function:) -> 也许检查你传递给那个 function 的内容:)

this code works perfectly此代码完美运行

I needed to pass the map the body twice as u can see in the code and to json encode it afterwards,我需要将 map 主体传递两次,就像您在代码中看到的那样,然后再对 json 进行编码,

then i had to pass the header params as it follow here然后我必须通过 header 参数,如下所示

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

Full code:完整代码:

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");
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM