gpt4 book ai didi

http - flutter 如何处理 http 服务器端验证错误

转载 作者:IT王子 更新时间:2023-10-29 06:46:00 25 4
gpt4 key购买 nike

我正在尝试在登录表单上显示服务器端错误。我正在尝试在数据可用时更改状态。但它不工作。谁能帮忙

或者有什么更好的方法吗?

 Future<dynamic> apiRequest(map) async {
String url = 'https://localhost/api/login';
var response = await http.post(Uri.encodeFull(url),
body: map, headers: {"Accept": "application/json"});
var res = json.decode(response.body);
return res;
}

void _submit() async {
if (this._formKey.currentState.validate()) {
_formKey.currentState.save(); // Save our form now.

var map = {
'email_id': '',
'password': '',
};
var hello = await apiRequest(map);

setState(() {
email_id_error = hello["errors"]["email_id"];
});

} else {
setState(() {
_autovalidate = true;
});
}
}

new Text(email_id_error),

服务器响应

{status_code: 4003, errors: {password: [can't be blank], email_id: [can't be blank]}}

最佳答案

你可以试试这个

void _submit() async {
if (this._formKey.currentState.validate()) {
_formKey.currentState.save(); // Save our form now.
var map = {
'email_id': '',
'password': '',
};
var hello = await apiRequest(map);
if(hello.statusCode ==4003){
setState(() {
email_id_error = hello.responseBody["errors"]["email_id"];
});
}
} else {
setState(() {
_autovalidate = true;
});
}
}

关于http - flutter 如何处理 http 服务器端验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52162112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com