颤抖如何处理HTTP服务器端验证错误

时间:2018-09-04 08:34:49

标签: http dart flutter

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

还是有更好的方法来做到这一点。

 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]}}

1 个答案:

答案 0 :(得分:0)

您可以尝试

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