Sub topic 10
Per-flavor Firebase and service config files
This chapter assumes you can run a Flutter app, understand Android application IDs and iOS bundle IDs, and have already seen --flavor and appFlavor. The goal is to give each environment its own Firebase project or service configuration without duplicating the Flutter application code.
Match every environment to the correct service app
A Firebase project can contain separate Android and iOS apps. Each app is identified by a package name or bundle ID. These identifiers must match the flavor that will use the configuration.
For example:
| Environment | Android package name | iOS bundle ID | Firebase project |
|---|---|---|---|
| dev | `com.example.myapp.dev` | `com.example.myapp.dev` | `myapp-dev` |
| prod | `com.example.myapp` | `com.example.myapp` | `myapp-prod` |
Download the Android configuration as google-services.json and the iOS configuration as GoogleService-Info.plist. Do not use the production file for the development flavor. Firebase may reject the configuration, or the development app may write data into production services.
Android source sets
The Google Services Gradle plugin selects a configuration file from the most specific Android source set. Place one file in each flavor directory:
android/app/
└── src/
├── dev/
│ └── google-services.json
└── prod/
└── google-services.jsonThe dev file must contain an Android Firebase app whose package name is com.example.myapp.dev. The prod file must contain an app whose package name is com.example.myapp.
When you run flutter run --flavor dev, Gradle builds the dev variant and reads android/app/src/dev/google-services.json. A production build reads the file under src/prod.
iOS schemes and configuration files
iOS uses schemes and build configurations rather than Android source-set folders. Store the files separately, then make each scheme copy or include the matching file:
ios/config/
├── dev/GoogleService-Info.plist
└── prod/GoogleService-Info.plistA common setup gives each scheme a build configuration such as Debug-dev or Release-prod. An Xcode build phase can copy the selected file into the application bundle. Another option is to add both files to the Xcode project and use target membership or configuration-specific resources.
The important rule is that the final app bundle contains exactly one GoogleService-Info.plist, and its bundle ID matches the selected scheme. Keeping both files in the project without controlling which one is bundled can produce a build that connects to the wrong Firebase project.
Use generated Flutter options
The FlutterFire CLI can generate Dart options for each Firebase project. Generate separate files, then select one at startup:
flutterfire configure --project=myapp-dev --platforms=android,ios --out=lib/firebase_options_dev.dart
flutterfire configure --project=myapp-prod --platforms=android,ios --out=lib/firebase_options_prod.dartThe generated files contain platform-specific Firebase identifiers. They are configuration, not authentication credentials.
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'firebase_options_dev.dart' as dev;
import 'firebase_options_prod.dart' as prod;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final FirebaseOptions options = switch (appFlavor) {
'dev' => dev.DefaultFirebaseOptions.currentPlatform,
'prod' => prod.DefaultFirebaseOptions.currentPlatform,
_ => throw StateError('Build with --flavor dev or --flavor prod'),
};
await Firebase.initializeApp(options: options);
runApp(const MaterialApp(
home: Scaffold(body: Center(child: Text('Firebase is ready'))),
));
}This Dart selection and the native file selection must agree. For example, a dev build should select the development Dart options and the development native plist or JSON file.
>callout warning >Firebase configuration files identify an application and its backend project. They are not a safe place for private server credentials, database passwords, or administrative service-account keys. Keep true secrets on a server or in a protected CI secret store.
Verify the selected configuration
Run each flavor and inspect Firebase Console data, such as a test Analytics event or development document. Build both variants in CI as well. A useful failure check is to compare the application ID, bundle ID, Firebase project ID, and flavor name before publishing.
Exercise
Why can a development flavor need both a different google-services.json file and different Dart FirebaseOptions?
Show answer
The native file configures Android or iOS integration, while Dart FirebaseOptions configures the Flutter Firebase initialization. Both must point to the same environment.