Flutter: Firebase has not been correctly initialized
Asked Answered
A

7

24

I'm working on iPhone 12 Pro Max Emulator, macOS Catalina.

I'm getting this error when I try to run the app:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.

Also there is a tip in the console: Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp.

I initialize the Firebase before use it. Like this:

void main() async {
  print('-- main');

  WidgetsFlutterBinding.ensureInitialized();
  print('-- WidgetsFlutterBinding.ensureInitialized');

  await Firebase.initializeApp();
  print('-- main: Firebase.initializeApp');

  runApp(const MyApp());
}

This is what I see in the console output:

Xcode build done.                                           132.9s
flutter: -- main
flutter: -- WidgetsFlutterBinding.ensureInitialized
[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`.

I can't see the -- main: Firebase.initializeApp line in the console. So it fails in first trying to initialize the Firebase.

I create Android/Apple apps in Firebase. Downloaded google-services.json / GoogleService-Info.plist and put in the project.

  • GoogleService-Info.plist:

iOS

  • google-services.json:

Android

I'm not using the android, but I added dependency into build.gradle: classpath 'com.google.gms:google-services:4.3.10'

And app/build.gradle: apply plugin: 'com.google.gms.google-services'

dependencies:

firebase_auth: ^3.3.5
firebase_messaging: ^10.0.9
google_sign_in: ^5.2.1

flutter --version:

Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (3 months ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4

How can I solve this problem? BTW, I'm working on a brand new flutter project.

Alderson answered 7/1, 2022 at 22:9 Comment(3)
When you added google-services.json -- did u add it through Xcode? If yes, did you restart your app?Linhliniment
I added it to the android folder in project manually. Yes I restarted the app a few times.Creator
I just updated my answer because you also need to add firebase_core to the dependencies.Linhliniment
L
47

When you add google-services.json to an iOS project, you need to add it using Xcode as described in the following document:

https://firebase.flutter.dev/docs/manual-installation/ios

If you read through the page, you'll find the following note:

adding [google-service.json] manually via the filesystem won't link the file to the project

You need to try that then restart your app (rebuild it).

Edit: Additional Note:

You'll also need to add firebase_core to your dependencies in pubspec.yaml.

Linhliniment answered 7/1, 2022 at 22:39 Comment(8)
Thanks mate! After fixing this, there is another problem. App crashes in the await _googleSignIn.signIn()line. And there is no error message. Do you know why is that crash?Creator
@UmutÇağdaşCoşkun anytime. I haven't used google_sign_in package before. Maybe it's worth posting a separate question and add it to the Flutter Community on Twitter (that's how I found yours btw)Linhliniment
Okay, I'm checking a few topics in SO. I will post a new question if they will not work for me. Thanks again! 👊Creator
@UmutÇağdaşCoşkun If you are using Google Sign In then make sure you have specified "Support Email" by going to Project settings > General > Public settings.Platinotype
Excelent answer!Nut
Strangely enough, Copying the file worked for iPhones but not iPad, got the firebase initialization error only for the iPad and even strange, same copying worked in two other projects in iPad too. Nonetheless, your answer worked.Infrequent
OMFG man. Thank you sooo so much. It's so stupid you have to do it from XCode.... Nobody tells you this in the Firebase instructionsCachexia
Yes, It is worked As if we add the google-service.json file directly to the android studio, it is not showing in the Xcode so we have to import the file manually In the Xcode.Hansiain
H
15

You have to add the GoogleService-Info.plist in Xcode instead of just placing it into the folder:

Click on Runner > Add Files to "Runner"

So it looks like this:

GoogleServices-Info.plist directly inside of the "Runner" project

Note: The file can also be moved to a sub folder in the project (i.e. Runner).

Source: https://www.kindacode.com/article/flutter-correctly-adding-googleservices-info-plist-to-ios/

Hamnet answered 6/9, 2022 at 21:34 Comment(0)
C
8

Here's how I fixed this error:

  1. Ensure that all firebase services have been added to your pubspec.yaml file, in the dependencies section. firebase_core appears to be missing and is required to connect your flutter app to your firebase project.

You can simply add it using this command:

   flutter pub add firebase_core 
  1. Add the firebase plugins to your main file:

    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
    
  2. Replace your void main function with an asynchronous one:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const YourAppGoesHere());
    } 
    
Champaign answered 7/3, 2022 at 14:34 Comment(0)
T
5

When I add the GoogleService-Info.plist file in Xcode, I used the wrong name GoogleService-Info**(1)**.plist. If you have the same file in downloads, mac adds a number of copy to the next downloaded file.

Tommyetommyrot answered 30/4, 2022 at 11:3 Comment(0)
C
1

I encountered the same error while trying to initialize Firebase correctly in my Flutter project. Here's the correct way to do it:

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

Additionally, I added the following dependencies to my pubspec.yaml file:

firebase_core: ^2.15.1
firebase_analytics: ^10.4.5

The issue I faced was that I had not used the CLI to add Firebase to my project. Instead, I was configuring Android and iOS separately, which caused problems. To bypass this issue, I made the following adjustments:

Added the following key to my Info.plist file (located under ios > Runner):

<key>CFBundleIdentifier</key>
<string>YourIOSBundleID</string>

Placed the GoogleServices-info.plist file into the Runner directory.

To ensure proper integration, I opened Xcode and manually dragged the GoogleServices-info.plist file into the Runner folder.

By implementing these steps, I was able to initialize Firebase in my Flutter project without needing to explicitly add the line:

   await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform, // No need to add this line
    );

This resolved the issue and allowed me to use Firebase functionalities in my app.

Coucal answered 7/9, 2023 at 8:32 Comment(0)
N
1

The DefaultFirebaseOptions.currentPlatform is not accessible in firebase now. Instead you have to use FirebaseOptions()

await Firebase.initializeApp(options: FirebaseOptions(apiKey: '',appId: '',messagingSenderId: '',projectId: ''));

All these values are necessary. You will get the apikey, appid and projectId, in the google-services.json file and also on the project settings page under general tab, and for messagingSenderId, you need to go to cloud messaging tab, on the project settings page, and there you will find it under sender ID.

Neurologist answered 26/10, 2023 at 20:18 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Convulsant
F
0

For me , i was missing the keyword await infront of Firebase.initialiseApp()

Foreworn answered 4/7, 2023 at 21:42 Comment(1)
The questioner is already using await keyword. Could you please either update/edit your answer or delete it? Thanks.Franni

© 2022 - 2024 — McMap. All rights reserved.