Firestore : config from native apps

Flutter tips Published on

Managing different setups (like dev, staging, prod) for Firebase in your Flutter app can be tricky. But there's a cool way to handle it all directly in Dart, letting the firebase plugin use configurations right from your code.

Why use this method?

Easily switch between your different Firebase environments like development, staging, and production. Keep your configurations clean and in one place (your Dart code), making management simpler.

How to set it up

Step 1: Setup for Each Environment

Create a folder, perhaps named runners, at the root of your project. Inside runners, make separate .dart files for each environment you need (e.g., main_dev.dart, main_staging.dart, main_prod.dart). Each of these files will have a main function that calls a helper function, passing in the specific configuration for that environment.

Step 2: Create Your Configuration

Make a file for your FlavorConfig class. This class will hold the Firebase configuration options specific to each environment. The flutterfire cli can help you generate the FirebaseOptions needed. You'll likely need to install it (dart pub global activate flutterfire_cli) and then run flutterfire configure in your project. Your FlavorConfig will provide the correct FirebaseOptions based on the environment.

Step 3: Update Your Main File

Remove the standard runApp call from your main lib/main.dart file. Add a new function, like runWrapperApp, that takes the configuration object as input. This function will first make sure Flutter widgets are initialized (WidgetsFlutterBinding.ensureInitialized()), then initialize Firebase using Firebase.initializeApp with the options from your configuration, and finally call runApp with your main App widget.

Step 4: Run with Your Environment

When you want to run your app for a specific environment, use the -t flag to point to the main file for that environment within the runners folder. For example, to run the development version, you'd use a command like: flutter run -t lib/runners/main_dev.dart

Important Note

Using the flutterfire cli to generate options adds native platform configurations. It's crucial to remove any old, manually added Firebase configuration files from your native project folders (like GoogleService-Info.plist on iOS and google-services.json on Android). Also, remember to remove the google-services dependency line from your android/app/build.gradle file to avoid conflicts.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why should I configure Firebase using Dart?

Configuring Firebase through Dart makes managing different environments (development, staging, production) much easier and keeps your configuration settings clean and centralized in your code.

Do I still need `google-services.json` or `GoogleService-Info.plist`?

No, when you use this method with the `flutterfire cli`, you should remove these native files and any related dependencies in your native build files (like `build.gradle`).

What is `flutterfire cli`?

It's a command-line tool provided by the FlutterFire team that helps you set up and manage Firebase within your Flutter project, including generating the necessary configuration options.

Read more
You may also be interested in
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets  blog card image
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets
Published on 2025-05-12T08:10:13.136Z
input keyboard actions  blog card image
input keyboard actions
Published on 2025-05-12T11:46:24.531Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved