Capstone: Real-World Flutter Project

Firebase Integration & Project Configuration

16 min Lesson 2 of 10

Firebase Integration & Project Configuration

Firebase is Google's comprehensive app development platform that provides a suite of backend services — Firestore (NoSQL database), Authentication, Storage, Cloud Functions, and more — all accessible from Flutter without managing any server infrastructure. In this lesson you will connect your capstone Flutter project to a Firebase project using the FlutterFire CLI, configure the three core services (Firestore, Authentication, and Storage), and set up environment-specific options for both Android and iOS.

Step 1 — Create a Firebase Project

Before writing any code, you need a Firebase project in the Google Firebase Console:

  • Go to console.firebase.google.com and click Add project.
  • Give it a name (e.g., capstone-app), optionally enable Google Analytics, and click Create project.
  • Once created, enable the three services: Firestore Database, Authentication (enable Email/Password provider), and Storage.
Note: Keep Firestore in test mode initially so you can read and write freely during development. Before shipping to production you must tighten the security rules.

Step 2 — Install FlutterFire CLI & firebase_core

FlutterFire CLI automates platform-specific configuration (generating google-services.json for Android and GoogleService-Info.plist for iOS) and produces a single firebase_options.dart file that your app uses at runtime.

Install dependencies

# Install the Firebase CLI globally (if not already installed)
npm install -g firebase-tools
firebase login

# Install the FlutterFire CLI globally
dart pub global activate flutterfire_cli

# Add required packages to your Flutter project
flutter pub add firebase_core cloud_firestore firebase_auth firebase_storage
Tip: Make sure ~/.pub-cache/bin is on your PATH so that the flutterfire command is found. On macOS/Linux, add export PATH="$PATH":"$HOME/.pub-cache/bin" to your shell profile.

Step 3 — Run flutterfire configure

With the CLI installed, run the following command from the root of your Flutter project. It will prompt you to select your Firebase project and automatically configure both Android and iOS:

Configure FlutterFire

flutterfire configure --project=capstone-app

The CLI will:

  • Download and place android/app/google-services.json for Android.
  • Download and place ios/Runner/GoogleService-Info.plist for iOS.
  • Generate lib/firebase_options.dart — a Dart file containing a DefaultFirebaseOptions class with platform-specific API keys and project IDs.
Warning: Never commit google-services.json, GoogleService-Info.plist, or firebase_options.dart to a public repository. Add them to .gitignore. These files contain API keys that can be abused if exposed.

Step 4 — Initialize Firebase in main.dart

Firebase must be initialized before runApp(). Because initialization is asynchronous, main() must be async and you must call WidgetsFlutterBinding.ensureInitialized() first:

lib/main.dart — Firebase initialization

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

Future<void> main() async {
  // Must be called before any async work in main()
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(const MyApp());
}

DefaultFirebaseOptions.currentPlatform automatically selects the correct configuration (Android vs iOS vs Web) at runtime based on the detected platform.

Step 5 — Configure Android & iOS Platform Files

Beyond the JSON/plist files, each platform needs a small one-time setup.

Android — Open android/build.gradle and add the Google Services plugin classpath, then apply it in android/app/build.gradle:

android/build.gradle (project level)

buildscript {
  dependencies {
    // Add this line:
    classpath 'com.google.gms:google-services:4.4.1'
  }
}

android/app/build.gradle (app level) — bottom of file

// Apply the plugin at the very bottom:
apply plugin: 'com.google.gms.google-services'

iOS — Run cd ios && pod install after adding the Firebase packages so CocoaPods pulls the native Firebase SDKs. Also set a minimum iOS deployment target of 13.0 in ios/Podfile:

ios/Podfile

platform :ios, '13.0'

Step 6 — Accessing Firebase Services

Once initialized, each service is accessed via its singleton instance. Here is a concise reference:

Accessing Firestore, Auth & Storage singletons

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';

// Firestore — the NoSQL database
final FirebaseFirestore db = FirebaseFirestore.instance;

// Authentication
final FirebaseAuth auth = FirebaseAuth.instance;

// Cloud Storage (file uploads)
final FirebaseStorage storage = FirebaseStorage.instance;

// Example: write a document to Firestore
Future<void> saveUser(String uid, String name) async {
  await db.collection('users').doc(uid).set({
    'name': name,
    'createdAt': FieldValue.serverTimestamp(),
  });
}

Environment-Specific Configuration

Production apps typically use separate Firebase projects for development and production to avoid polluting live data with test writes. Run flutterfire configure once per environment and save each generated firebase_options.dart under different filenames, then select the right one with a Dart define at build time:

Selecting config via --dart-define

# Build for production
flutter run --dart-define=FLAVOR=production

# In lib/firebase_options_loader.dart:
import 'firebase_options_dev.dart' as dev;
import 'firebase_options_prod.dart' as prod;

const String _flavor =
    String.fromEnvironment('FLAVOR', defaultValue: 'dev');

FirebaseOptions get currentPlatformOptions {
  return _flavor == 'production'
      ? prod.DefaultFirebaseOptions.currentPlatform
      : dev.DefaultFirebaseOptions.currentPlatform;
}

Summary

You have connected your Flutter capstone project to Firebase end-to-end. The key steps are: create a Firebase project and enable services in the console, install the FlutterFire CLI and run flutterfire configure to generate platform config files, call Firebase.initializeApp() before runApp(), apply the Google Services Gradle plugin for Android, and run pod install for iOS. The generated DefaultFirebaseOptions.currentPlatform ensures the correct keys are used on every platform without any manual conditional logic in your Dart code.