【问题标题】:Flutter Failed to handle method call. java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't existFlutter 处理方法调用失败。 java.lang.IllegalStateException:名称为 [DEFAULT] 的 FirebaseApp 不存在
【发布时间】:2020-07-03 11:12:38
【问题描述】:

我的应用程序中的 firebase 有问题。我正在我的应用程序中使用 firebase 进行 OTP。但是当我尝试发送电话号码时遇到了这个问题:

E/MethodChannel#plugins.flutter.io/firebase_auth(19880): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:218)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.getAuth(FirebaseAuthPlugin.java:80)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.onMethodCall(FirebaseAuthPlugin.java:160)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/flutter (19880): isValid - true
I/flutter (19880): mobiel +90151515
E/flutter (19880): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, FirebaseApp with name [DEFAULT] doesn't exist. , null)

我在 android/app.xml 中添加了 google-services.json。我已经在 android/build.gradle 中添加了这些依赖项,并且还尝试更改不同的版本,但应用程序仅适用于 4.1.0:

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // Add the google services classpath
        classpath 'com.google.gms:google-services:4.1.0'
    }

在android/app/build.gradle的最后还添加了apply plugin:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.firebase:firebase-core:17.0.0'
}

apply plugin: 'com.google.gms.google-services'

这是我的源代码:

void _onVerifyCode() async {
    setState(() {
      isCodeSent = true;
    });
    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential phoneAuthCredential) {
      _firebaseAuth
          .signInWithCredential(phoneAuthCredential)
          .then((AuthResult value) {
        if (value.user != null) {
          // Handle loogged in state
          print(value.user.phoneNumber);
          Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) => HomeWidget(
                  user: value.user,
                ),
              ),
              (Route<dynamic> route) => false);
        } else {
          showToast("Error validating OTP, try again", Colors.red);
        }
      }).catchError((error) {
        showToast("Try again in sometime", Colors.red);
      });
    };
    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      showToast(authException.message, Colors.red);
      setState(() {
        isCodeSent = false;
      });
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      _verificationId = verificationId;
      setState(() {
        _verificationId = verificationId;
      });
    };
    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      _verificationId = verificationId;
      setState(() {
        _verificationId = verificationId;
      });
    };

    // TODO: Change country code

    await _firebaseAuth.verifyPhoneNumber(
        phoneNumber: "+90${widget.mobileNumber}",
        timeout: const Duration(seconds: 60),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  void _onFormSubmitted() async {
    AuthCredential _authCredential = await PhoneAuthProvider.getCredential(
        verificationId: _verificationId, smsCode: _pinEditingController.text);

    _firebaseAuth
        .signInWithCredential(_authCredential)
        .then((AuthResult value) {
      if (value.user != null) {
        // Handle loogged in state
        print(value.user.phoneNumber);
        Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(
              builder: (context) => HomeWidget(
                user: value.user,
              ),
            ),
            (Route<dynamic> route) => false);
      } else {
        showToast("Error validating OTP, try again", Colors.red);
      }
    }).catchError((error) {
      showToast("Something went wrong", Colors.red);
    });
  }

Flutter clean 效果不佳!

【问题讨论】:

    标签: firebase authentication flutter


    【解决方案1】:

    解决了

    我做了我的依赖: 类路径 'com.google.gms:google-services:4.3.3'

    并更改了我的 firebase 包: 来自:firebase_auth:^0.16.0 收件人:firebase_auth:^0.15.0

    然后重新运行整个应用程序

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 2017-04-25
      • 1970-01-01
      • 2019-12-04
      • 2016-09-17
      • 2016-12-07
      • 2021-10-17
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多