Flutter:Firebase 在 Flutter 应用程序中向我发送错误代码

Flutter: Firebase send me a wrong code in flutter app

我正在使用 Firebase_auth 程序包来验证 phone 号码,Firebase 发送代码但是当我检查代码的有效性时它只是显示错误。当我打印发送的代码时,它与我收到的代码不同。任何人都知道请帮助我。 提前致谢。 这是我的代码:

await FirebaseAuth.instance.verifyPhoneNumber(
                      timeout: Duration(seconds: 60),
                      phoneNumber: phoneNumber,
                      verificationCompleted:
                          (PhoneAuthCredential phoneAuthCredential) {
                        print(phoneAuthCredential.smsCode);
                        print("complete");
                      },
                      verificationFailed: (e) {
                        print("failed");
                        print(e);
                      },
                      codeSent: (same, a) {
                        print(a);
                        print(same);
                        Navigator.of(context).pushNamed(
                          VerifyScreen.routName,
                          arguments: {
                            'code': a,
                            'customerId': customerId,
                          },
                        );
                      },
                      codeAutoRetrievalTimeout: (sa) {
                        print(sa);
                        print("timeout");
                      },
                    );

我知道收到的代码无法打印,但它以加密形式 verificationId。

验证输入的代码后,将用户输入的代码和 verificationId 发送到 Firebase 进行验证。

如果您的目标是通过此verificationCompleted无需输入代码即可自动验证,您可以自动转移用户。

Note: Provided that the sim card is on the same phone to which the code was sent.

附加信息:如果您希望该过程是自动的,请使用以下代码:

  Future singInPhoneNumber() async {
    FirebaseAuth _auth = FirebaseAuth.instance;

    _auth.verifyPhoneNumber(
      phoneNumber: phoneNumber,
      timeout: Duration(seconds: 60),
      verificationCompleted: (AuthCredential phoneAuthCredential) async {

          //Automatically verify

                 Navigator.of(context).pushNamed(
                      VerifyScreen.routName,
                      arguments: {
                        'code': a,
                        'customerId': customerId,
                      },
                    ); 
        debugPrint("Firebase Auth :Auto Verification Completed");
      },
      verificationFailed: (FirebaseAuthException error) {
        debugPrint("ERROR Firebase Auth : AuthException : ${error.message}");

        showToast(context, 'Something went wrong, please try again later');
      },
      codeSent: (String verificationId, [int forceResendingToken]) {

        setState(() {
          this.verificationId = verificationId;
        });
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        return null;
      },
    );
  }

如果您只想手动输入代码:

AuthCredential credential = PhoneAuthProvider.credential(
        verificationId: verificationId, smsCode: codeController.text);

    _auth
        .signInWithCredential(credential)
        .then((value) => {
                      print('The operation completed successfully') 
            })
        .catchError((onError) {
      print(onError.toString());
    });