【问题标题】:Throwing but not catching投但不接
【发布时间】:2020-01-05 12:08:43
【问题描述】:

我正在尝试使用 flutter-fire 包执行 Firebase 电话身份验证(使用 SMS),但在“验证”过程中检查错误情况时遇到了一些障碍。

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

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

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

`

Future<void> verifyPhoneNumber(String phoneNumber) async {
    print("Verify Phone Number called");
    final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) {
      this._verificationID = verId;
    };

    final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) {
      this._verificationID = verId;
      print("SMS Sent");
      this.smsSent = true;
      notifyListeners();
      throw (HttpException("Test"));
    };

    final PhoneVerificationCompleted verificationSuccessful =
        (AuthCredential user) {
      print("verified");
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException exception) {
      print("failed, ${exception.message}");
      throw (exception);
    };

    this._phoneNumber = phoneNumber;
    notifyListeners();

    var result = await FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber: phoneNumber,
      codeAutoRetrievalTimeout: autoRetrieve,
      codeSent: smsCodeSent,
      timeout: const Duration(seconds: 0),
      verificationCompleted: verificationSuccessful,
      verificationFailed: verificationFailed,
    );

    return result;
  }`

这是我有捕获代码的地方:

`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");
    }
  }

`

编辑:我正在使用https://dart.dev/codelabs/async-await#example-async-and-await-with-try-catch 中的代码游乐场,我发现只有在 Future 返回某些内容时才会捕获异常,即使返回类型是 Future。这可能是根本原因吗?

【问题讨论】:

  • 你找到答案了吗?我现在有类似的问题(

标签: firebase flutter firebase-authentication


【解决方案1】:

你需要捕获你抛出的异常。

 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");
    }
  }

【讨论】:

  • 我以为catch是一个可以捕获所有异常的网?无论如何,我试过这个,我没有看到行为上有什么不同。是否与 throw 和 catch 在 2 个不同的文件中有关?
  • 它可以包罗万象 - 只是 throw('some error') 而不是 HttpException。 @vybhavr
  • 我正在这样做:final PhoneVerificationFailed verificationFailed = (AuthException exception) { print("failed, ${exception.message}"); throw (exception); };
  • 我正在使用dart.dev/codelabs/… 中的代码游乐场,我发现只有在Future 返回某些内容时才会捕获异常,即使返回类型是Future。这可能是根本原因吗?
  • 好的。我会试试的,不过我现在正在运行另一个错误。 “等待”之后的语句不等待异步任务完成。这是在异步函数中。 await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: phoneNumber, codeAutoRetrievalTimeout: autoRetrieve, codeSent: smsCodeSent, timeout: const Duration(seconds: 0), verificationCompleted: verificationSuccessful, verificationFailed: verificationFailed, ); print("Future error return called"); return Future.error(AuthErrorException);
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 2021-07-04
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多