Niklas Raab
Niklas Raab

Reputation: 1736

Dart is stepping over lines of code, when await is used in async method

In my Flutter project I have following code:

if(await model.login(loginMailTextController.text, 
   loginPasswordTextController.text)){
   Navigator.of(context).pushReplacementNamed("/main");
}

This code calls this function in my model:

Future<bool> login(String mail, String password) async {
   _auth.signInWithEmailAndPassword(email: mail, password: password);
   return true; //Makes no sense, only for testing
}

Which works as expected and the navigator method is called, but if I add the await before the signInWithEmailAndPassword method:

Future<bool> login(String mail, String password) async {
   await _auth.signInWithEmailAndPassword(
   email: mail, password: password);
   return true; //Debugger won't stop there when a breakpoint is set
}

Then the expression in the if statement is false. Also when a breakpoint is set on the marked line, the debugger don't stop. Setting a breakpoint onto the signInWithEmailAndPassword method works like charme.

Is this a bug or do I make a mistake?

Upvotes: 5

Views: 1553

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

"if statement is false" looks like await _auth.signInWithEmailAndPassword( throws and the exception is not reported or similar.

Try to wrap the code with try/catch

Upvotes: 4

Related Questions