当在 async 方法中使用 await 时,Dart 会跳过代码行

Nik*_*aab 5 dart android-studio flutter

在我的 Flutter 项目中,我有以下代码:

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

这段代码在我的模型中调用了这个函数:

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

它按预期工作并调用了 navigator 方法,但是如果我在方法之前添加awaitsignInWithEmailAndPassword

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
}
Run Code Online (Sandbox Code Playgroud)

那么 if 语句中的表达式为假。此外,当在标记行上设置断点时,调试器不会停止。在signInWithEmailAndPassword方法上设置断点就像魅力一样。

这是一个错误还是我犯了一个错误?

Gün*_*uer 4

“if 语句为 false”看起来像await _auth.signInWithEmailAndPassword(抛出异常,并且未报告异常或类似的情况。

尝试用 try/catch 包装代码