The error “No Firebase App DEFAULT has been created – call Firebase.initializeApp()” occurs when you try to use a Firebase service without first initializing Firebase. This error can occur in both Flutter and Node.js apps.
In this article, you’ll learn why and how to Solve No Firebase App DEFAULT has been created – call Firebase.initializeApp() in Flutter or Node JS. We will also provide some additional tips for avoiding this error.
Table of Contents
Solve No Firebase App DEFAULT has been created – call Firebase.initializeApp() in Flutter
The error “No Firebase App DEFAULT has been created – call Firebase.initializeApp()” occurs when you try to use a Firebase service in your Flutter app without first initializing Firebase.
There are two main reasons why this error might occur:
- You have not included the
firebase_core
package in your pubspec.yaml file. - You have not called the Firebase.initializeApp() function in your code.
To fix this error, follow these steps:
- Make sure that the
firebase_core
package is included in your pubspec.yaml file. After adding firebase_core, it must like like the one below.
name: my_app
description: A Flutter app that uses Firebase Core.
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_core: ^1.20.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
- Add the following code to your main.dart file:
import 'package:firebase_core/firebase_core.dart';
void main() async {
// Initialize Firebase
await Firebase.initializeApp();
// Run your app
runApp(MyApp());
}
Once you have done this, you should be able to use Firebase services in your Flutter app without any errors.
Solve No Firebase App DEFAULT has been created – call Firebase.initializeApp() in Node JS APP
To solve the error “No Firebase App DEFAULT has been created – call Firebase.initializeApp()” in a Node.js app, follow these steps:
- Install the Firebase Admin SDK for Node.js:
npm install firebase-admin
- Import the FirebaseAdmin module:
const firebase = require('firebase-admin');
- Initialize Firebase:
await firebase.initializeApp({
projectId: 'YOUR_PROJECT_ID',
});
Using Firebase services
For example, to use the Firebase Realtime Database, you would do the following:
const db = firebase.database();
// Get a reference to the database
const ref = db.ref('/my-database');
// Read data from the database
ref.on('value', (snapshot) => {
console.log(snapshot.val());
});
// Write data to the database
ref.set({
name: 'John Doe',
});
You can also use the following code to check if Firebase has been initialized before you use any Firebase services:
if (!firebase.apps.length) {
// Firebase has not been initialized
throw new Error('Firebase must be initialized before using any Firebase services');
}
This will help to ensure that you always initialize Firebase before you use any of its services.
Additional Tips to Avoid the Error
Here are some additional tips for avoiding this error.
- Make sure that you initialize Firebase before you use any Firebase services.
- If you are using multiple Firebase services, make sure that you initialize them all in the same place.
- If you are using a custom Firebase configuration, make sure that you pass it to the
firebase.initializeApp()
function as shown below.
const firebaseConfig = {
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
projectId: 'YOUR_PROJECT_ID',
storageBucket: 'YOUR_STORAGE_BUCKET',
messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
appId: 'YOUR_APP_ID',
};
firebase.initializeApp(firebaseConfig);
Conclusion
By following the steps in this article, you should be able to solve the error “No Firebase App DEFAULT has been created – call Firebase.initializeApp()” in both Flutter and Node.js apps.
Also, ensure that you are using the correct versions of Firebase SDK and Node.js. Outdated or incompatible Firebase SDK versions can cause this error. You can check your Firebase SDK version in your package.json
file and check for multiple initializations because Initializing Firebase more than once can lead to conflicts and this error. You should initialize Firebase only once when your application starts.