Firebase 扔而不抓

Firebase 扔而不抓,firebase,flutter,firebase-authentication,Firebase,Flutter,Firebase Authentication,我试图使用Flatter fire软件包执行Firebase电话身份验证(使用SMS),但在“验证”过程中检查错误条件时遇到了一些障碍 注意:抛出发生在ChangeNotifierProvider中,捕获发生在使用状态数据提供程序的单独小部件中 当我尝试调试时,我看到抛出了错误,但是没有调用catch块 这是引发错误的地方(PhoneVerificationFailed函数): ` 未来验证电话号码(字符串电话号码)异步{ 打印(“验证拨打的电话号码”); 最终PhoneCodeAutoRetr

我试图使用Flatter fire软件包执行Firebase电话身份验证(使用SMS),但在“验证”过程中检查错误条件时遇到了一些障碍

注意:抛出发生在ChangeNotifierProvider中,捕获发生在使用状态数据提供程序的单独小部件中

当我尝试调试时,我看到抛出了错误,但是没有调用catch块

这是引发错误的地方(PhoneVerificationFailed函数):

`

未来验证电话号码(字符串电话号码)异步{
打印(“验证拨打的电话号码”);
最终PhoneCodeAutoRetrievalTimeout autoRetrieve=(字符串verId){
这一点。_verificationID=verId;
};
final PhoneCodeSent smsCodeSent=(字符串verId,[int forceCodeResend]){
这一点。_verificationID=verId;
打印(“发送短信”);
this.smsSent=true;
notifyListeners();
抛出(HttpException(“测试”);
};
最终电话验证完成验证成功=
(身份验证凭据用户){
打印(“验证”);
};
最终电话验证失败验证失败=
(AuthException异常){
打印(“失败,${exception.message}”);
抛出(异常);
};
这个。_phoneNumber=phoneNumber;
notifyListeners();
var result=等待FirebaseAuth.instance.verifyPhoneNumber(
电话号码:电话号码,
codeAutoRetrievalTimeout:autoRetrieve,
代码发送:smsCodeSent,
超时:常量持续时间(秒数:0),
验证完成:验证成功,
验证失败:验证失败,
);
返回结果;
}`
这就是我的捕获代码:

`Future<void> _submitPhoneNumber() async {
    _formKey2.currentState.save();
    setState(() {
      _isLoading = true;
    });
    try {
      await Provider.of<Auth>(context, listen: false)
          .verifyPhoneNumber(_authData['phoneNo']);
      setState(() {
        _isLoading = false;
      });
    } catch (error) {
      print("Exception caught ${error.message}");
      errorSnackBar(error.message, "Close");
    }
  }
`Future\u submitPhoneNumber()异步{
_formKey2.currentState.save();
设置状态(){
_isLoading=true;
});
试一试{

wait Provider.of,我发现只有当Future返回某些内容时才会捕获异常,即使返回类型是Future。这可能是根本原因吗?

您需要捕获正在抛出的异常

 try {
      await Provider.of<Auth>(context, listen: false)
          .verifyPhoneNumber(_authData['phoneNo']);
      setState(() {
        _isLoading = false;
      });
  } on HttpException catch (e) {   // add on HttpException 
      print("Exception caught ${error.message}");
      errorSnackBar(error.message, "Close");
    }
  }
试试看{
wait Provider.of(上下文,侦听:false)
.verifyPhoneNumber(_authData['phoneNo']);
设置状态(){
_isLoading=false;
});
}在HttpException上捕获(e){//添加HttpException
打印(“捕获到异常${error.message}”);
errorSnackBar(error.message,“Close”);
}
}

您需要捕获正在抛出的异常

 try {
      await Provider.of<Auth>(context, listen: false)
          .verifyPhoneNumber(_authData['phoneNo']);
      setState(() {
        _isLoading = false;
      });
  } on HttpException catch (e) {   // add on HttpException 
      print("Exception caught ${error.message}");
      errorSnackBar(error.message, "Close");
    }
  }
试试看{
wait Provider.of(上下文,侦听:false)
.verifyPhoneNumber(_authData['phoneNo']);
设置状态(){
_isLoading=false;
});
}在HttpException上捕获(e){//添加HttpException
打印(“捕获到异常${error.message}”);
errorSnackBar(error.message,“Close”);
}
}

我以为捕获是捕获所有异常的网络?不管怎样,我试过了,但我没有发现行为上的差异。它是否与抛出和捕获在两个不同的文件中有任何关系?它可以捕获所有这些内容-只是
throw('some error')
而不是
HttpException
@vybhavrI正在这样做:
final PhoneVerificationFailed verificationFailed=(AuthException异常){print(“失败,${exception.message}”);throw(异常);}我在游戏场中玩代码游戏,我发现只有当Future返回某个东西时才会捕获异常,即使返回类型是Future。这可能是根本原因吗?好的。我会试试的,不过我正在运行另一个错误。“wait”后面的语句没有等待异步任务完成。这是在异步函数中<代码>等待FirebaseAuth.instance.verifyPhoneNumber(电话号码:phoneNumber,codeAutoRetrievalTimeout:autoRetrieve,codeSent:SMSCODensent,超时:常量持续时间(秒数:0),验证完成:验证成功,验证失败:验证失败,);打印(“调用未来错误返回”);返回Future.error(AuthErrorException)我以为捕获是捕获所有异常的网络?不管怎样,我试过了,但我没有发现行为上的差异。它是否与抛出和捕获在两个不同的文件中有任何关系?它可以捕获所有这些内容-只是
throw('some error')
而不是
HttpException
@vybhavrI正在这样做:
final PhoneVerificationFailed verificationFailed=(AuthException异常){print(“失败,${exception.message}”);throw(异常);}我在游戏场中玩代码游戏,我发现只有当Future返回某个东西时才会捕获异常,即使返回类型是Future。这可能是根本原因吗?好的。我会试试的,不过我正在运行另一个错误。“wait”后面的语句没有等待异步任务完成。这是在异步函数中<代码>等待FirebaseAuth.instance.verifyPhoneNumber(电话号码:phoneNumber,codeAutoRetrievalTimeout:autoRetrieve,codeSent:SMSCODensent,超时:常量持续时间(秒数:0),验证完成:验证成功,验证失败:验证失败,);打印(“调用未来错误返回”);返回Future.error(AuthErrorException)