Flutter can't handle 422 response?

Viewed 141

I am trying to handle 422 response code from my api, as you can see in console on wrong inputs I am getting 422, but whenever I try to check it with response.statusCode == 422 the code in that if statement doesn't work at all. I tried printing a lot of details there still can't see anything in it. Am I doing something wrong? Also I am returning a future with response from my method, so it's there for sure. It works for the 200 code but it doesn't handle 422

enter image description here

    onTap: () async {
                                    
                                    if (_formKey.currentState!.validate()) {
                                      try {
                                        var response = await context
                                            .read<Login>()
                                            .login(userData);

                                        if (response.statusCode == 200) {
                                          await context
                                              .read<Auth>()
                                              .fetchUser();
                                          Navigator.pushNamed(context, "/init");
                                        } else if (response.statusCode == 422) {
                                        // nothing happens here even though response is 422
                                          print("testttttttttttttttt");
                                          setState(() {
                                            validateData = true;
                                          });
                                        }
                                      } catch(err) {
                                        throw Exception("Server error");
                                      }
                                    }
0 Answers
Related