【问题标题】:The method 'getCredential' isn't defined for the type 'PhoneAuthProvider'没有为“PhoneAuthProvider”类型定义方法“getCredential”
【发布时间】:2021-01-25 14:56:08
【问题描述】:

据我所知,flutter firebase 身份验证中有许多已弃用的方法。

这是我的代码;

            FlatButton(
              child: Text("Confirm"),
              textColor: Colors.white,
              color: Colors.blue,
              onPressed: () async {
                FirebaseAuth _auth = FirebaseAuth.instance; // I didn't use it here. I did for you to see
                final code = _codeController.text.trim();
                final AuthCredential credential = PhoneAuthProvider.getCredential(
                        verificationId: verificationId, smsCode: code);

                UserCredential result =
                    await _auth.signInWithCredential(credential);

                User user = result.user;

                if (user != null) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) =>
                              HomeScreen(user: user)));
                } else {
                  print("Error");
                }
              },
            )

我尝试进行电话身份验证,但 getCredential 有错误。我查看了 firebase 文档。它说不推荐使用 PhoneAuthProvider,我应该将 PhoneAuthCredential 与 getCredential 一起使用。我用过但没用。我看到在 stackoverflow 上有关于这个错误的各种问题,但我找不到解决方法

【问题讨论】:

标签: firebase flutter firebase-authentication


【解决方案1】:

这就是我在firebase_auth: ^0.18.0+1 上的应用中实现它的方式:

  @override
  Future<void> signInWithSmsCode(String smsCode) async {
    final AuthCredential authCredential = PhoneAuthProvider.credential(
      smsCode: smsCode,
      verificationId: _verificationCode,
    );
    try {
      await auth.signInWithCredential(authCredential);
    } on PlatformException catch (e) {
      throw _firebaseErrorFactory.getException(e.code);
    } on FirebaseAuthException {
      throw _firebaseErrorFactory
          .getException('ERROR_INVALID_VERIFICATION_CODE');
    }
  }

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2021-05-24
    • 2021-09-02
    • 2022-11-21
    • 2021-10-19
    • 2020-12-14
    • 2021-07-19
    • 2021-09-01
    • 2021-09-17
    相关资源
    最近更新 更多