Home > Net >  How to check if getting the api was successful or not
How to check if getting the api was successful or not

Time:09-18

I am checking the login process and I want to know if the process was successful or not

   login() async{


       var f=formstate.currentState;
            if (f!.validate()) {
       var response=  await crud.postrecuest(linklogin, {
  "email":email.text,
  "password":password.text
});

Navigator.pushNamed(context, "Home");




            

            

            }
  

}

CodePudding user response:

you can use try-catch

Futures and error handling

Ex:

try {
      var response=  await crud.postrecuest(linklogin, {
     "email":email.text,
     "password":password.text
    });

    }catch(error) {

   your code here : What code to execute if an error occurs

    }
   
   // -> If there is no error, we will reach this line
   Navigator.pushNamed(context, "Home");

CodePudding user response:

You can also check the status code of the response. First, you have to make sure what is the statusCode of the response to successful login (probably 200). Then check if your response has such a status code.

Code:

var response=  await crud.postrecuest(linklogin, {
 "email":email.text,
 "password":password.text
});
if (response.statusCode != 200) {
  throw LoginFailure();
} else {
  Navigator.pushNamed(context, "Home");
}
  • Related