← Back to Fixes

Fix "No Firebase App [DEFAULT] has been created" in Flutter

Fix the "No Firebase App [DEFAULT] has been created" error by correctly initializing the Firebase app before calling any Firebase services in Flutter.

· Updated

The error is blunt about the cause, even if the cause is easy to miss:

text
FirebaseException: [core/no-app] No Firebase App '[DEFAULT]' has been created.
Call Firebase.initializeApp() before using any Firebase services.

It fires the moment you touch FirebaseFirestore.instance, FirebaseAuth.instance, or FirebaseStorage.instance before the default Firebase app has been registered. Flutter's Firebase SDK does not auto-initialize the way Android's native SDK does — you have to call Firebase.initializeApp() yourself, and await it, before any service accessor runs. The invariant is simple: the app instance must exist before any service looks it up.

The error, decoded#

The stack appears the first time a widget reads a Firebase service. Three situations produce it reliably:

  • You add Firebase logic inside a widget's build() or initState() without guaranteeing initialization has completed first.
  • You move Firebase calls into a service class and instantiate it at the top level of a file, before main() has finished initializing.
  • You edit main() and only hot-reload — hot reload preserves Dart VM state, so the new main() never re-runs and the app stays uninitialized.

It is identical across Android, iOS, and web builds. And importantly, it is not about credentials or a misconfigured google-services.json — your config files can be perfect and you still hit this if initializeApp() is not called in the right order.

Why Flutter doesn't auto-init Firebase#

Firebase.initializeApp() registers a FirebaseApp instance named [DEFAULT] in the SDK's internal registry. When you later call FirebaseFirestore.instance, the SDK looks up that registry; if there is no [DEFAULT] app, it throws. A typical broken main() looks like this:

dart
// lib/main.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
 
void main() async {
  // WRONG: This line is missing or not awaited
  // await Firebase.initializeApp();
 
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({super.key});
 
  @override
  Widget build(BuildContext context) {
    // This line throws the error if Firebase.initializeApp() hasn't run
    final db = FirebaseFirestore.instance;
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Firestore instance: $db'),
        ),
      ),
    );
  }
}

FirebaseFirestore.instance tries to resolve the default app before it has been registered. The same applies to FirebaseAuth.instance, FirebaseStorage.instance, and every other Firebase service — they all go through the same registry lookup.

The fix: initialize before runApp#

Register the default app in main() before anything can read it:

dart
// lib/main.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
 
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({super.key});
 
  @override
  Widget build(BuildContext context) {
    final db = FirebaseFirestore.instance;
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Firestore instance: $db'),
        ),
      ),
    );
  }
}

WidgetsFlutterBinding.ensureInitialized() prepares the Flutter engine for async work, then await Firebase.initializeApp() registers the default app before runApp() renders anything. Two prerequisites in pubspec.yaml:

yaml
dependencies:
  flutter:
    sdk: flutter
  firebase_core:
  cloud_firestore:

Run flutter pub get after editing it. Then flutter run — the app launches without the [DEFAULT] error and the Firestore instance prints cleanly.

Two patterns that still trip you up#

A service class instantiated at the top level. This is the most common way the error survives a "fixed" main():

dart
// lib/services/firestore_service.dart
import 'package:cloud_firestore/cloud_firestore.dart';
 
class FirestoreService {
  final db = FirebaseFirestore.instance; // touched at construction time
 
  Future<void> saveData() async {
    await db.collection('users').add({'name': 'Alice'});
  }
}
 
// elsewhere
final service = FirestoreService(); // runs before main() finishes init

FirebaseFirestore.instance is evaluated when the global service is constructed — before main() has awaited initializeApp(). Defer the access until after initialization:

dart
// lib/services/firestore_service.dart
import 'package:cloud_firestore/cloud_firestore.dart';
 
class FirestoreService {
  FirebaseFirestore? _db;
 
  Future<void> init() async {
    _db ??= FirebaseFirestore.instance;
  }
 
  Future<void> saveData() async {
    await _db!.collection('users').add({'name': 'Alice'});
  }
}
dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  final service = FirestoreService();
  await service.init();
  runApp(MyApp(service: service));
}

Hot reload versus hot restart. Hot reload preserves Dart VM state, so adding await Firebase.initializeApp() while the app is already running does not re-execute main(). The error persists until you do a full hot restart. After editing main(), always hot-restart, not hot-reload.

Confirm it's initialized#

Add a one-line log to prove the app registered before the UI mounts:

dart
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
 
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  print('Firebase app initialized: ${Firebase.app().name}');
  runApp(const MyApp());
}

You should see Firebase app initialized: [DEFAULT] in the console. If that line prints and the error is gone, the invariant holds: the app instance exists before any service touches it.