【问题标题】:Dart is stepping over lines of code, when await is used in async method当在异步方法中使用 await 时,Dart 正在跳过代码行
【发布时间】:2019-03-16 10:56:33
【问题描述】:

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

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

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

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

按预期工作并调用导航器方法,但如果我在 signInWithEmailAndPassword 方法之前添加 await

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
}

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

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

【问题讨论】:

  • 你在说哪个if声明?
  • 我也遇到过这种情况,不过几个月后它变得更好了。在带有await 的行之前添加print('foo'); 行通常(并非总是)有助于使调试器表现得更好。
  • @RémiRousselet ´if(await model.login(loginMailTextController.text, loginPasswordTextController.text))´
  • @GünterZöchbauer 你向 dart 或 Flutter 团队报告了吗?
  • @GünterZöchbauer 最后一条评论是解决方案,我只是在 VS Code 中打开它并抛出了异常,只有在 Android Studio 中没有。谢谢你的帮助!写一个答案,我会接受并投票!

标签: android-studio dart flutter


【解决方案1】:

“if statement is false”看起来像await _auth.signInWithEmailAndPassword( throws 并且没有报告异常或类似情况。

尝试用 try/catch 包装代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多