【问题标题】:How can I catch the error when the wrong SMS code is entered in Flutter?在 Flutter 中输入错误的 SMS 代码时如何捕获错误?
【发布时间】:2021-02-05 07:02:37
【问题描述】:

我在 Firebase 平台中使用电话身份验证。实际上我用下面的代码解决了这个问题,但是 Firebase 给了我另一个类似的错误。

我正在尝试 2 个选项;其中一个正在使用 catch(e),另一个正在使用 FirebaseAuthException catch (me),但我总是遇到同样的错误。

  signin() async {
    final AuthCredential credential = PhoneAuthProvider.credential(
        verificationId: verificationId, smsCode: code);
    try {
      await FirebaseAuth.instance.signInWithCredential(credential);
      print("successful");
    } catch (e) {
      print("not successful");
    }    
  }

发生了异常。 PlatformException (PlatformException(invalid-verification-code, 用于创建手机认证凭证的短信验证码无效。请重新发送验证码短信并务必使用用户提供的验证码。, {code: invalid-verification -code、message:用于创建手机认证的短信验证码无效,请重新发送验证码短信,一定要使用用户提供的验证码,nativeErrorMessage:用于创建手机的短信验证码auth凭证无效,请重新发送验证码短信,务必使用用户提供的验证码,nativeErrorCode:17044,additionalData:{}}))

【问题讨论】:

    标签: android ios firebase flutter firebase-authentication


    【解决方案1】:

    您可以通过FirebaseAuthException代码过滤错误;

    signin() async {
      final AuthCredential credential = PhoneAuthProvider.credential(
        verificationId: verificationId,
        smsCode: code,
      );
      try {
        await FirebaseAuth.instance.signInWithCredential(credential);
        print("successful");
      } on FirebaseAuthException catch (e) {
        if (e.code == 'invalid-verification-code') {
          onWrongCode();
        }
        onOtherError(e);
      } catch (e) {
        onOtherError(e);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2023-02-13
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      相关资源
      最近更新 更多