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

jv2fixgn  于 2023-04-22  发布在  Flutter
关注(0)|答案(4)|浏览(181)

我使用这个链接作为教程: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));
  }

救命啊!

uxhixvfz

uxhixvfz1#

在程序的开始(主函数)检查是否有firebaseapp的示例。
您的异常是“instance of firebase_auth is null”,没有firebaseapp,Firebase Auth无法工作。[firebase_auth/unknown] null

nbnkbykc

nbnkbykc2#

将“void main()”更新为“Future main()”

void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    runApp(MyApp());
}

Future main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(
   options: DefaultFirebaseOptions.currentPlatform);// dont include "options: DefaultFirebaseOptions.currentPlatform" if you havn't used Flutter Fire
   runApp(MyApp());
}
s1ag04yj

s1ag04yj3#

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

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

tpgth1q7

tpgth1q74#

Firebase有授权尝试限制。请在一天后重试。

相关问题