【问题标题】:How can I return variable from FirebaseAuth phone verification method in flutter?如何在颤振中从 FirebaseAuth 电话验证方法返回变量?
【发布时间】:2019-10-02 20:11:50
【问题描述】:

我正在使用 FirebaseAuth 电话验证,我的问题是我正在使用 bloc 模式,所以我希望 FirebaseAuth 电话验证方法向 Bloc 返回一些变量,以指示用户是否已经存在以导航到电话验证屏幕与否。

这是我的手机验证方法,注意这个函数在另一个类中,不在Bloc中:

 static sendPhoneVerificationMessage(String phone) async {
    AuthCredential credential;
    final PhoneVerificationCompleted verificationCompleted = (AuthCredential user){
      print('Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: '+user.toString());
    };

    final PhoneVerificationFailed verificationFailed = (AuthException authException){
      print('Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
          verificationCode = verificationId;
      print("code sent to " + phone);
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      verificationCode = verificationId;
      print("time out");
    };

      await FirebaseAuth.instance.verifyPhoneNumber(
          phoneNumber: phone,
          timeout: const Duration(seconds: 120),
          verificationCompleted: verificationCompleted,
          verificationFailed: verificationFailed,
          codeSent: codeSent,
          codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

  }

这是 bloc 中调用前一个函数的函数

  static sendPhoneVerificationMessage(String phone) async {
    String formattedPhone = "+2" + phone;
    await AuthAPI.sendPhoneVerificationMessage(formattedPhone);
  }

我想要的是根据 sendPhoneVerificationMessage 函数的返回执行一些操作

【问题讨论】:

    标签: firebase flutter firebase-authentication flutter-dependencies sms-verification


    【解决方案1】:

    我已经像您尝试的那样使用了带有 bloc 模式的 FirebaseAuth 电话验证。

    就我而言,我使用 Stream 进行控制。 我有一个枚举,其中包含验证过程的所有可能状态,并且对于每个状态我调用_stream.sink.add(MyEnum.CURRENT_STATE)。 (你可以在回调中做这个控制。)

    最后,在我看来,监听器会根据传递给流的步骤更改 UI。

    [免费提示]
    小心你通过的超时。如果您通过的超时时间不为零,则应用程序将尝试自动获取短信中的验证码。如果发生这种情况,您将陷入verificationCompleted回调,您的验证码将自动失效,您将无法继续使用。

    您可以正确控制这种情况,让用户进入verificationCompleted 方法,或者您可以将零传递给超时并禁用此行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-12
      • 2021-03-22
      • 2020-04-19
      • 2020-11-11
      • 2020-06-06
      • 2021-02-27
      • 2019-10-13
      相关资源
      最近更新 更多