'Flutter: Firebase has not been correctly initialized

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.



Solution 1:[1]

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.

Solution 2:[2]

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 the command $flutter pub add firebase_core

  2. Add the firebase plugins to your main file:

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

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const YourAppGoesHere());
    } 
    

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ahmed Ashour
Solution 3 Enviro Apps