未处理的异常:[firebase_auth/unknown] null:使用 flutter/firebase 进行电话身份验证时出错

时间:2021-06-05 14:20:25

标签: firebase flutter firebase-authentication

我使用此链接作为教程:https://firebase.flutter.dev/docs/auth/phone/ 但不知何故,当我按下按钮时,我收到了这个错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: [firebase_auth/unknown] null
E/flutter ( 6765): #0      MethodChannelFirebaseAuth.verifyPhoneNumber (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:583:7)
E/flutter ( 6765): <asynchronous suspension>
E/flutter ( 6765): #1      _OTPScreenState._verifyMyPhoneNumber (package:food_delivery_app/UIForAllCategory/PhoneAuth/OTPScreen.dart:41:5)
E/flutter ( 6765): <asynchronous suspension>
E/flutter ( 6765): 
D/ViewRootImpl[Toast]( 6765): hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false

我已在 Firebase 启用电话身份验证,并重新下载了 GoogleServices 文件,但始终面临此错误。 这是按下按钮时运行的第一个函数:

  _verifyMyPhoneNumber() async {
    //This handler will only be called on Android devices which support automatic SMS code resolution.
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: '+92${this.widget.phoneNumber}',
        verificationCompleted: (PhoneAuthCredential credential) async {
          await FirebaseAuth.instance
              .signInWithCredential(credential)
              .then((value) async {
            if (value.user != null) {
              //Change state and move to next Screen
              setState(() {
                this.widget.inProgress=false;
              });
              String myNumber=this.widget.phoneNumber;
              debugPrint("My number= $myNumber");
              prefs.setString("phone", "0$myNumber");

              //Getting values from preferences
              String number= prefs.getString("phone");
              String email=  prefs.getString("email");
              String username=  prefs.getString("username");
              String uid= prefs.getString("Gid");
              debugPrint("Details: $number, $email, $username,$uid");
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => EnteryPoint()),
              );
            }
          });
        },


        //In case, if Some error occured at Firebase then
        verificationFailed: (FirebaseAuthException e) {

          //Change state and Go back with a prompt
          setState(() {
            this.widget.inProgress=false;
          });
          Fluttertoast.showToast(
              msg: "Error: $e",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.CENTER,
              timeInSecForIosWeb: 2,
              backgroundColor: Colors.black87,
              textColor: Colors.white,
              fontSize: 12.0);
          Navigator.of(context).pop(); //Go back to previious screen
        },


        codeSent: (String verficationID, int resendToken) {
          debugPrint("Code is sent from Server, Soon you will recieve it");
          //Change state and Let user to enter the code when received
          setState(() {
            this.widget.inProgress=false;
          });
          Fluttertoast.showToast(
              msg: "Please wait, You will soon receive the code!",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.CENTER,
              timeInSecForIosWeb: 2,
              backgroundColor: Colors.black87,
              textColor: Colors.white,
              fontSize: 12.0);
          setState(() {
            _verificationCode = verficationID;
          });
        },
        codeAutoRetrievalTimeout: (String verificationID) {
          debugPrint("This device has not automatically resolved an SMS message within a certain Timeframe");
          setState(() {
            _verificationCode = verificationID;
          });
        },
        timeout: Duration(seconds: 60));
  }

请帮忙!

2 个答案:

答案 0 :(得分:0)

检查在程序开始时是否有 firebaseapp 实例(主函数)。 使用 await Firebase.initializeApp(); 创建实例;

您的例外是“firebase_auth 的实例为空”,如果没有 firebaseapp,Firebase 身份验证将无法工作。 未处理的异常:[firebase_auth/unknown] null

答案 1 :(得分:0)

FirebaseAuth auth=FirebaseAuth.instance;
auth.userChanges();
auth.instance.verifyPhoneNumber(
//code`enter code here`
);

如果您尝试从一台设备多次登录