简体   繁体   中英

Firebase is not getting initialized right

So, I wanted to include firebase into my Flutter App, it works fine for android but when I start it on my ios emulator it will always give me this error

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized. Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp . View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization

I have the google-service file in the right directory and I followed the tutorial and edited my main() to:

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

Has anybody the same problem with IOS or did I do anything wrong?

So for everybody else getting this error I figured it out myself, for IOS, the line

  Firebase.initializeApp()

it needs an option:

Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)

for that follow the instructions on this link:

https://firebase.flutter.dev/docs/cli

hope that helps anyone

  1. .Once installed, the flutterfire command will be globally available.
  2. Run flutterfire configure in your flutter project folder in terminal,
  3. Select a Firebase project to configure your Flutter application with,
  4. Select/Insert android app id and bundle id you want to use as and when it asks you.
  5. and get firebase_options.dart.
  6. In main.dart file import firebase_options.dart.
  7. In void main() use following code.

        WidgetsFlutterBinding.ensureInitialized();
         if (Platform.isIOS) {
         await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform,
         );
           } else {
             await Firebase.initializeApp();
             }
        runApp(
        const MaterialApp(
        home: MyApp(),
        ),
      );

I got same problem. My Android application is working fine but when using IOS application it's throw this type of exception. you can use @Viki's answer and if its not working for you make sure you have added GoogleServices-info.plist file in your ios project. If it's not solve your issue then use this code

if (Platform.isIOS) {
    await Firebase.initializeApp(
        options: FirebaseOptions(
            apiKey: "your api key Found in GoogleService-info.plist",
            appId: "Your app id found in Firebase",
            messagingSenderId: "Your Sender id found in Firebase",
            projectId: "Your Project id found in Firebase"));
  } else {
    await Firebase.initializeApp();
  }

In my case, I haven't added the code lines according to the instructions from firebase. The simple fix is to follow the instructions from the firebase doc from start to finish.

在此处输入图像描述

I have faced the same issue in my flutter application when run on xcode/ ios. Here is how I solved.

  1. Download GoogleService-Info.plist from firebase account
  2. Add Files to Runner like below and choose GoogleService-Info.plist from download folder

在此处输入图像描述

  1. Re-Run the project, It will work

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