简体   繁体   中英

Error when trying to authenticate users with google in firebase

I've been struggling with this for months, sinc.e I'm just learning firebase with flutter. I have a simple button that when pressed should give the option to register or start section with google, but when running I get errors in the console.

There are so many that I don't know which one it is.

Error: Assertion failed:
file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.2/lib/src/firebase_core_web.dart:207:11
options != null
"FirebaseOptions cannot be null when creating the default app."

Error: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

在此处输入图像描述

here in onpress of the button:

child: MaterialButton(
          onPressed: () async {
            User? user = await Authenticator.IniciarSesion(context: context);
            print(user?.displayName);
          },

and the class that authenticates

class Authenticator {
  static Future<User?> IniciarSesion({required BuildContext context}) async {
    FirebaseAuth authenticator = FirebaseAuth.instance;
    User? user;
    GoogleSignIn objGooglesignIn = GoogleSignIn();
    GoogleSignInAccount? objGooglesignInAccount =
        await objGooglesignIn.signIn();

    if (objGooglesignInAccount != null) {
      GoogleSignInAuthentication objGoogleSignInAuthentication =
          await objGooglesignInAccount.authentication;
      AuthCredential credential = GoogleAuthProvider.credential(
          accessToken: objGoogleSignInAuthentication.accessToken,
          idToken: objGoogleSignInAuthentication.idToken);
      try {
        UserCredential userCredential =
            await authenticator.signInWithCredential(credential);
        user = userCredential.user;
        return user;
      } on FirebaseAuthException catch (e) {
        print("error de auth");
      }
    }
  }
}

Sorry if I put a lot of code. I don't know exactly what the error could be.

There are two issues with your application, first one seems like you are not initializing firebase in the main function properly. Please refer mentioned link for setup.

Flutter firebase setup guide

Additionally as I can see there is no firebase_options.dart in your lib directory which basically manages (api key) configurations for different platforms.

If you have followed steps mentioned in guide correctly you can create these configs using

flutterfire configure

command only after you have already installed flutterfire cli using following command

dart pub global activate flutterfire_cli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM