英文:
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: {"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
答案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('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(),
}),
);
print('response: ${response.body}'); // this line shows you the response
if (response.statusCode == 200) {
final jsonResponse = json.decode(response.body);
if (jsonResponse["success"] == true) {
Navigator.pop(context);
}
} else {
print('Request failed with this status code: ${response.statusCode}.');
}
}
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('response: ${response.body}');
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论