30

I am getting the above error when calling Firebase.initializeApp() in my flutter code. I have followed the documentation here: https://firebase.flutter.dev/docs

Here is my pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.5.0
  cloud_firestore: ^0.14.0
  firebase_auth: ^0.18.0
  fl_chart: ^0.11.0
  snapping_sheet: ^2.0.0
  flutter_svg: ^0.18.0
  flutter_redux: ^0.6.0
  strings: ^0.1.2
  random_string: ^2.1.0
  redux_thunk: ^0.3.0
  #  firebase_crashlytics: ^0.1.4+1
  dotted_line: ^2.0.1

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3

dev_dependencies:
  flutter_test:
    sdk: flutter

Here is my flutter code:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);

  final store = Store<AppState>(AppState.reducer, initialState: AppState.initial(), middleware: [thunkMiddleware]);

  runApp(
    FutureBuilder(
      future: Firebase.initializeApp(),
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          log(snapshot.error.toString());
          return Container(color: Colors.red);
        }

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {
          return StoreProvider<AppState>(
            store: store,
            child: MoollaApp(store: store),
          );
        }

        // Otherwise, show something whilst waiting for initialization to complete
        return Container(color: Colors.green);
      },
    ),
  );
}

Here are the relevant part of my .gradle (app) file:

plugins {
    id "com.android.application"
    id "com.google.gms.google-services"
    id "kotlin-android"
    id "kotlin-android-extensions"
}

Here is my project gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0-alpha07'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and my Application class extension:

class MyApp : FlutterApplication() {
    override fun onCreate() {
        super.onCreate()
    }
}

As far as I can tell I have done everything correctly according to the docs.

John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
Tom Taila
  • 531
  • 1
  • 5
  • 11

16 Answers16

10

Setting minifyEnabled & shrinkResources to false fixes it but why? I do not know. If somebody know please write a comment.

buildTypes {       
    release {
        signingConfig signingConfigs.release
        minifyEnabled false
        shrinkResources false
    }
}
Mehmet Filiz
  • 608
  • 5
  • 18
  • This saved my bacon big style. I had released a production upgrade having set `minifyEnabled true` (amongst other things) which resulted in a black screen ANR. Changing it back to `false` fixed this issue. Cheers Mehmet – Simon Hutton Aug 26 '21 at 10:06
  • Insane. Thanks a lo @Mehmet you saved my life. I've been having this issue for 4 days now and it was driving me crazy! – Migalv Dec 18 '21 at 00:42
10

Recently Added Firebase

Many Firebase packages include native code. In general any package with native code requires a complete reset of the app on the device; meaning deleting initial values and executing native code for the first time:

  1. Uninstall the app from the device
  2. Close and reopen Android Studio (sometimes optional)
  3. Run/Start the application again

Done!

Paul
  • 1,349
  • 1
  • 14
  • 26
6

It's difficult to say exactly what's wrong without sifting through your entire project, but here are a few actions that will likely remedy the problem.

  1. Run flutter doctor and verify everything is working as expected.
  2. Run flutter clean followed by flutter pub get.

If you're still receiving the same error, then you likely made a mistake while editing one of the config files.

  1. Compare your android/app/main/AndroidManifest.xml to the previous working version (specifically ensure you didn't accidentally delete any <meta-data /> tags as doing so would result in said error.
  2. Do the same with the android/build.gradle and android/app/build.gradle.
4

What I ended up realizing after running into this issue, is that the error is super vague and doesn't necessarily mean there's a problem with firebase_core, rather there could be an issue with any of the Firebase plugins. So to narrow down where the issue was, I went through each firebase import individually and commented it out in the Pubspec.yaml file and ran it (along with commenting out all the code in my project that used that import), until I found which package was causing the issue.

For me, I was experiencing the issue only when running on iOS. It turned out that the firebase_messaging was the culprit and it was code that I had added to the iOS AppDelegate to handle the messaging that was no longer needed with the latest version of the firebase_messaging plugin.

odiggity
  • 1,496
  • 16
  • 29
  • 3
    In my case, it was the flutter_apns plugin and the problem only occurred on iOS. I've then noticed that it provides the specific flag "flutter_apns.disable_firebase_core" (mentioned in the readme) to enable the parallel use of firebase core. – Jeff S. Mar 17 '21 at 11:13
1

I started a fresh project and made sure to change: classpath 'com.android.tools.build:gradle:4.2.0-alpha07' to the version officially supported: 3.5.0 Not sure if that downgrade was necessary because as I say, I restarted an entirely fresh project and something else may have effected it.

I also filed a ticket with Google which may prove helpful for anyone else in the future: https://github.com/FirebaseExtended/flutterfire/issues/3212#issuecomment-675407420

Tom Taila
  • 531
  • 1
  • 5
  • 11
  • This solved my problem thank you , in build.gradle file , make sure you use a version no earlier than classpath 'com.android.tools.build:gradle:4.1.0' – Jay Shenawy Jul 01 '22 at 13:30
1

To resolve this problem you have to implements this dependency for Android setup.

firebase_core 

if you have this error for desktop application then you have to implement this dependency

firebase_core_desktop: ^1.0.0
Rishita Joshi
  • 203
  • 2
  • 3
0

I did a bit of research on the issue and found an issue that was opened and closed with the agreed solution being to simply create a new flutter application and porting your dependencies and files one after the other. While this was not a viable solution for me, running

flutter clean

then

flutter pub get

seemed to fix the issue. As of yet I am unsure what package exactly causes the issue but on my deployment I had

google_sign_in: ^4.5.5
firebase_core: ^1.2.0
firebase_auth: ^1.2.0

only.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I had the same problem I then reinstalled the app and updated my app level build.gradle

defaultConfig{
// add the following line
 multiDexEnabled true
}
0

I had this issue. For me I forgot to create my project in firebase console properly, and i was trying to initialize my app like this, Firebase.initializeApp();

ABDUL SAMAD
  • 33
  • 1
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Vatsal Dholakiya Aug 31 '21 at 13:48
0

FlutterFire includes instructions and tools for testing SignIn, Storage and Firestore:

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

matwr
  • 1,548
  • 1
  • 13
  • 23
0

I had the same issue and above answers didn't helped that's why writing this.

Issue appeared when I changed my app's package name during development. So I pulled the recent previous code from git and updated package name in that folder which solved the issue for me. Still don't know what might have caused this issue!

0

Firebase.initializeApp() will throw error if you do not configure app with all the available platform.

Use below line of code

WidgetsFlutterBinding.ensureInitialized();

await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
);
Imran Vora
  • 47
  • 1
  • 3
0

Follow these steps to remove this issue :-

  1. do flutter clean
  2. then flutter pub cache repair
  3. then flutter pub get
  4. Run your application
Nikhil Soni
  • 701
  • 7
  • 12
0

What fixed it for me was checking installing firebase_core. It was missing from my pubspec.yaml

  • 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](/help/how-to-answer). – Community Jul 08 '22 at 09:00
0

I faced this now, and will add the steps which fixed it for me. I did NOT have to do a minifyEnabled false, or cut on any other performance. I can't say which steps were not needed, but hope this helps someone.

  1. Bumped implementation platform('com.google.firebase:firebase-bom:30.3.2') in app/build.gradle file. This forced me to bump compileSdkVersion to 33 as well in the same file.
  2. Bumped classpath 'com.android.tools.build:gradle:7.2.1 in android/build.gradle file.
  3. Bumped distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip in gradle-wrapper.properties
  4. After this I had to do a flutter pub upgrade --major-versions.
  5. Did a flutter clean on shell, and Invalidate and Restart on IDE. Even then I had some package conflict in firebase-messaging and firebase-auth, so I removed the versions from them in pubspec.yaml (just kept them as firebase_messaging: and firebase_auth: respt ). Voila, it started working!
beria
  • 153
  • 1
  • 6
-1

In my case, just follow the official documentations, first, run following command and install flutterfire_cli

dart pub global activate flutterfire_cli

you may need install "Firebase CLI" too. after installation, login to firebase, running

firebase login

and finaly run,

flutterfire configure

you can select the firebase project by menu and generate "firebase_options.dart" file. in your project add following

import 'firebase_options.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(MyApp());
}

full document https://firebase.flutter.dev/docs/overview/

9Dragons
  • 137
  • 1
  • 4