[Solved] No Firebase App ’[DEFAULT]’ has been created - call Firebase.initializeApp() Error in Flutter

While using Firebase services in Flutter, you may get "No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()" Error in Flutter. This error is caused when you use any Firebase services such as Cloud Messaging, Firestore without initializing Firebase core.

E/flutter (10040): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] 
Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
E/flutter (10040): #0      MethodChannelFirebase.app 
(package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:149:5)
E/flutter (10040): #1      Firebase.app (package:firebase_core/src/firebase.dart:55:41)
E/flutter (10040): #2      FirebaseMessaging.instance (package:firebase_messaging/src/messaging.dart:32:47)

Here, in our case, this error occurs while using Firebase cloud messaging.

The cause of this error is you might not have initialized firebase before using firebase service, or you may have initialized like below:

void main() async {
   WidgetsFlutterBinding.ensureInitialized();

  FirebaseMessaging.instance.subscribeToTopic("all"); 
  //use of Firebase service without initialization of Firebase core.
  
  runApp(MyApp());

}

OR

void main() async {
   WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp(); //initilization of Firebase app
  // here, Firebase.initilizeApp() is Future method, so you need to add await before.
  //run time error: Unhandled Exception: [core/no-app] 
  //No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
  
  runApp(MyApp());
}

To solve this error, add firebase_core Flutter package in your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^1.10.6

Now in main() method of main.dart file, initialize Firebase like below before using any Firebase services:

import 'package:firebase_core/firebase_core.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();
   //initilization of Firebase app
  
  // other Firebase service initialization

  runApp(MyApp());
}

Here, we have initialized Firebase using Firebase.initializeApp() method. Remember, this is a Future method, so don't forget to ass "await" before it, otherwise, you will encounter the same error.

In this way, you can solve "No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()" error in the Flutter app.

2 Comments on this Article

Pratik

It solved that error but still the app is not getting the connection and loading the else part of the application

1 year ago

Marwan

Thank you 
this code solved my Error

1 year ago


Please Wait...