17

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.

Umut Çağdaş Coşkun
  • 1,197
  • 2
  • 15
  • 33

5 Answers5

40

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.

osaxma
  • 2,657
  • 2
  • 12
  • 21
  • 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? – Umut Çağdaş Coşkun Jan 07 '22 at 23:24
  • 1
    @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) – osaxma Jan 07 '22 at 23:25
  • Okay, I'm checking a few topics in SO. I will post a new question if they will not work for me. Thanks again! – Umut Çağdaş Coşkun Jan 07 '22 at 23:35
  • 1
    @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`. – Souvik Biswas Jan 08 '22 at 02:48
  • Excelent answer! – F-Y Feb 08 '22 at 19:10
  • 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. – Lalit Fauzdar Sep 29 '22 at 20:43
  • 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 instructions – Mad World Nov 27 '22 at 20:23
  • 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. – khushbu Apr 11 '23 at 06:06
13

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/

Martin Braun
  • 10,906
  • 9
  • 64
  • 105
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());
    } 
    
Sharon Atim
  • 1,777
  • 16
  • 12
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.

DEFL
  • 907
  • 7
  • 20
KiriLL Sabko
  • 113
  • 1
  • 5
0

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

  • The questioner is already using await keyword. Could you please either update/edit your answer or delete it? Thanks. – Mearaj Jul 06 '23 at 01:59