如何在Flutter(Dart)中正确接收来自服务器的响应?

huangapple go评论52阅读模式
英文:

how to correctly receive a response from the server in flutter(dart)?

问题

I can't process the response from the server correctly in flutter. The user is created successfully in the database. In the postman application, I also get a response from the server: {"success":true}. But for some reason a connection error is displayed in the console, although the user is also successfully created from the flutter in the table:

Future<void> _sendLanguages(List<String> selectedLanguages, String firstName, String lastName, String email, String password) async {
    final url = Uri.parse('http://localhost/create_user.php');
    final response = await http.post(
      url,
      headers: {"Content-Type": "application/json"},
      body: jsonEncode({
        'first_name': widget.firstName,
        'last_name': widget.lastName,
        'email': widget.email,
        'password': widget.password,
        'languages': _selectedLanguages.map((language) => language.code).toList(),
      }),
    );
    final jsonResponse = json.decode(response.body);
    if (jsonResponse["success"]) {
      Navigator.pop(context);
    }
}

I tried changing it to success == true, but that also doesn't work. Dart doesn't properly process the response from the server.

英文:

I can't process the response from the server correctly in flutter. The user is created successfully in the database. In the postman application, I also get a response from the server: {&quot;success&quot;:true}. But for some reason a connection error is displayed in the console, although the user is also successfully created from the flutter in the table:

Future&lt;void&gt; _sendLanguages(List&lt;String&gt; selectedLanguages, String firstName, String lastName, String email, String password) async {
    final url = Uri.parse(&#39;http://localhost/create_user.php&#39;);
    final response = await http.post(
      url,
      headers: {&quot;Content-Type&quot;: &quot;application/json&quot;},
      body: jsonEncode({
        &#39;first_name&#39;: widget.firstName,
        &#39;last_name&#39;: widget.lastName,
        &#39;email&#39;: widget.email,
        &#39;password&#39;: widget.password,
        &#39;languages&#39;: _selectedLanguages.map((language) =&gt; language.code).toList(),
      }),
    );
    final jsonResponse = json.decode(response.body);
    if (jsonResponse[&quot;success&quot;]) {
      Navigator.pop(context);
    }
  } 

I tried changing it to success == true , but that also doesn't work. Dart doesn't properly process the response from the server

答案1

得分: 1

以下是您要翻译的内容:

"I think the error you have(and please share the error log), is not related to the response. but you can check the status code and see its behavior:

if the response code wasn't 200, you have a problem with your request(if you had checked the url and headers and your body that are correct).

and if the response code is 200 and you have a problem again, pay attention to this print('response: ${response.body}'); and see if you are parsing it in a correct way or not.
and if you still have a problem, you should provide more details to help you.

happy coding."

英文:

I think the error you have(and please share the error log), is not related to the response. but you can check the status code and see its behavior:

Future _sendLanguages(List selectedLanguages, String firstName, String lastName, String email, String password) async { 
final url = Uri.parse(&#39;http://localhost/create_user.php&#39;); 
final response = await http.post(
  url,
  headers: {&quot;Content-Type&quot;: &quot;application/json&quot;},
  body: jsonEncode({
    &#39;first_name&#39;: widget.firstName,
    &#39;last_name&#39;: widget.lastName,
    &#39;email&#39;: widget.email,
    &#39;password&#39;: widget.password,
    &#39;languages&#39;: _selectedLanguages.map((language) =&gt; language.code).toList(),
  }),
);


print(&#39;response: ${response.body}&#39;); // this line shows you the response

if (response.statusCode == 200) {
  final jsonResponse = json.decode(response.body);
  if (jsonResponse[&quot;success&quot;] == true) {
    Navigator.pop(context);
  }
} else {
  print(&#39;Request failed with this status code: ${response.statusCode}.&#39;);
 }
}

if the response code wasn't 200, you have problem with your request(if you had checked the url and headers and your body that are correct).

and if the response code is 200 and you have problem again, pay attention to this print(&#39;response: ${response.body}&#39;);
and see if you are parsing in a correct way or not.
and if you still had problem you should say more details to help you.

happy coding.

huangapple
  • 本文由 发表于 2023年3月8日 17:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定