Skip to content

Firebase.initializeApp() in Flutter

Problem

When integrating Firebase into a Flutter application, you may encounter the error:

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

This error typically occurs when trying to use Firebase services (like Authentication, Firestore, etc.) before initializing the Firebase SDK. The error indicates that Firebase hasn't been properly set up before attempting to use its features.

Common symptoms include:

  • App crashes when accessing Firebase services
  • Authentication, database, or other Firebase operations fail
  • The error appears in console logs when clicking buttons that trigger Firebase operations

Solution

1. Basic Initialization

The most straightforward solution is to initialize Firebase in your main.dart file before running the app:

dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

WARNING

Make sure to:

  1. Add the async keyword to the main() function
  2. Call WidgetsFlutterBinding.ensureInitialized() first
  3. Use await before Firebase.initializeApp()

2. Using FutureBuilder for Better UX

For a better user experience, use FutureBuilder to show a loading indicator while Firebase initializes:

dart
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: Firebase.initializeApp(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return ErrorWidget(); // Your error widget
        }
        
        if (snapshot.connectionState == ConnectionState.done) {
          return MyApp(); // Your main app
        }
        
        return LoadingWidget(); // Your loading widget
      },
    );
  }
}

For Flutter 2.8+ versions, use the FlutterFire CLI for simplified configuration:

bash
dart pub global activate flutterfire_cli
bash
flutterfire configure

This generates a firebase_options.dart file and handles platform-specific configuration.

Then use it in your main.dart:

dart
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

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

4. Platform-Specific Initialization

For specific platform requirements:

dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  if (Platform.isIOS) {
    await Firebase.initializeApp(
      options: const FirebaseOptions(
        apiKey: "your-api-key",
        appId: "your-app-id",
        messagingSenderId: "your-sender-id",
        projectId: "your-project-id",
      ),
    );
  } else {
    await Firebase.initializeApp();
  }
  
  runApp(MyApp());
}

Common Issues and Fixes

Missing firebase_core Dependency

Ensure you have the latest firebase_core package in your pubspec.yaml:

yaml
dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^2.13.0
  # Other Firebase packages

Incorrect Package Import

DANGER

Don't use the wrong package! Ensure you're importing the official Firebase package:

dart
import 'package:firebase_core/firebase_core.dart'; // Correct

Not:

dart
import 'package:firebase_core_dart/firebase_core_dart.dart'; // Incorrect

Web-Specific Configuration

For Flutter web, you need to include Firebase SDK in web/index.html:

html
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js"></script>
<!-- Add other Firebase products as needed -->

<script>
  var firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "..."
  };
  firebase.initializeApp(firebaseConfig);
</script>

Android Configuration

Ensure you have the Google services plugin in android/app/build.gradle:

gradle
apply plugin: 'com.google.gms.google-services'

Initialization Order

WARNING

Ensure Firebase initialization completes before any other async operations:

dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(); // This first
  await OtherService.initialize(); // Then others
  runApp(MyApp());
}

Best Practices

  1. Initialize once: Call Firebase.initializeApp() only once in your application
  2. Error handling: Wrap initialization in try-catch blocks for better error reporting
  3. Platform awareness: Use platform checks when different configurations are needed
  4. Dependency management: Keep Firebase packages updated to the latest versions
  5. Testing: Test initialization on all target platforms (iOS, Android, Web)

Migration from Legacy Setup

If migrating from older Firebase setups:

  1. Remove platform-specific config files (google-services.json, GoogleService-Info.plist)
  2. Remove related Gradle plugin entries
  3. Use the FlutterFire CLI for modern configuration

By following these guidelines, you should be able to resolve the "No Firebase App '[DEFAULT]' has been created" error and ensure proper Firebase initialization in your Flutter applications.