Switch between different Flutter environments.
Useful if you have multiple environments that all have different app id's, API endpoints and Firebase projects.
Install this packages globally:
npm i -g flutter-env
- Create the
flutter-env.json
and.flutter-env
file in the root of your Flutter project. - Run
flutter-env flutter-env-example
to see an example of theflutter-env.json
file and use this as the base for your file. - Commit these files.
- Run
flutter-env switch-to prod
to switch to theprod
environment (or some other environment you have defined in theflutter-env.json
file).
This file defines all properties for each environment. The default
environment must be present defines the default properties and their values. These values can be overruled in the other environments you define. For example below we have defined 3 environments: dev
, test
and prod
. The dev
environment overrules the app_id
and some env
variables. The test
and prod
environments only overrule the app_id
.
The app ID.
All variables that are defined in the env
section will be written to the file env_file
as static const
variables of the env_class
class.
See above - these are the variables of the env_class
class in the env_file
file.
{
"default": {
"env_file": "my_env.dart",
"env_class": "MyEnv",
"env": {
"apiProtocol": {
"value": "https",
"type": "String"
},
"apiHost": {
"value": "www.example.com",
"type": "String"
},
"sentryDns": {
"value": "https://sentry.com",
"type": "String"
}
}
},
"dev": {
"app_id": "my.dev.app",
"env": {
"apiProtocol": {
"value": "http"
},
"apiHost": {
"value": "localhost:8080"
}
}
},
"test": {
"app_id": "my.test.app"
},
"prod": {
"app_id": "my.prod.app"
}
}
If you want to use multiple Firebase projects for your environments, first follow the steps below. After that you can add the with-fb
option to the switch-to
command.
Execute the flutterfire configure
command. This will add the following files to your Flutter project:
- android/app/google-services.json
- ios/firebase_app_id_file.json
- ios/Runner/GoogleService-Info.plist
- lib/firebase_options.dart
Now move these 4 files and add _[environment] to it. For example if this is the dev
environment, the files will become:
- android/app/google-services_dev.json
- ios/firebase_app_id_file_dev.json
- ios/Runner/GoogleService-Info_dev.plist
- lib/firebase_options_dev.dart
Execute the following flutterfire command: flutterfire configure -p [project-name] -i [app-id] -a [app-id]
. The -i and -a are the app ids of the iOS and Android app. This will add again the 4 files to your Flutter project.
Now move these 4 files and add _[environment] to it. For example if this is the prod
environment, the files will become:
- android/app/google-services_prod.json
- ios/firebase_app_id_file_prod.json
- ios/Runner/GoogleService-Info_prod.plist
- lib/firebase_options_prod.dart
Now commit your files, so we have all files.
A basic check if you have the required files.
Switch to a specific environment.
For example to switch to the dev
environment:
flutter-env switch-to dev
Or switch to the myprod
environment:
flutter-env switch-to myprod
If you have set up Firebase as well, you need to aff the with-fb
option:
flutter-env switch-to myprod --with-fb
Lists the environments from the flutter-env.json
file and prepend the current environment with a *.
Prints help message.
Prints an example flutter-env.json
file.