Use this library from Activout AB to switch between different Firebase projects in your app.
When you have multiple Firebase projects for production, test and development, use this library to make it easier to switch between environments without building separate apps!
- Create separate Firebase projects for production, test and development (or what you prefer).
- Generate firebase_options files for each Firebase project with the FlutterFire CLI.
- Initialize this library in your main.dart according to the instructions below. It will call
Firebase.initializeApp()
for you!
See the example
subdirectory for a full example.
In your main.dart file, import all your firebase_options.dart
files generated
by FlutterFire CLI:
import 'package:example/firebase_options_production.dart' as production;
import 'package:example/firebase_options_test.dart' as test;
import 'package:example/firebase_options_development.dart' as development;
Update your main
method to be async
call FirebaseOptionsSelector.initialize()
with await
:
Future<void> main() async {
const productionKey = 'Production';
await FirebaseOptionsSelector.initialize(productionKey, {
productionKey: production.DefaultFirebaseOptions.currentPlatform,
"Test": test.DefaultFirebaseOptions.currentPlatform,
"Development": development.DefaultFirebaseOptions.currentPlatform,
});
runApp(const MyApp());
}
When you create your MaterialApp
, set the builder
property to add a banner to the app when it is not running with
production options:
return MaterialApp(
builder: FirebaseOptionsSelector.materialAppBuilder,
In a secret place, such as triple-tap on your logo, open the Firebase options selector dialog:
onPressed: () async {
await showFirebaseOptionsSelectorDialog(context, "Select environment and restart app");
},
Activout AB is a Swedish Flutter mobile app agency. Maybe we can help your company?