I am building a Flutter app, and I have variables with different values for different environments (QA, dev, prod, etc). What\'s a good way to organize my app so I can easil
The cleaner way to do this is through Build Flavors.
As a quick example, if you want to have a different app id for your app for your "dev" build, you could have this in the gradle file:
flavorDimensions "version"
productFlavors {
dev {
applicationIdSuffix ".dev"
versionNameSuffix "-dev"
}
}
Read more about gradle build variant configuration here.
Now you can run with this build variant with command line:
flutter run --flavor dev
If you are using Android Studio, you can set the build variant in the Run configuration too:
Read more about iOS configuration on this blog. And official flutter documentation about build flavours.
One way to do it: create different main_<environment>.dart
files in the lib/
directory of your project.
Each main_<environment>.dart
contains the environment-specific configurations/values (such as the different database names, etc). Each main_<environment>.dart
then imports the actual application library and runs the application, passing in the environment's values/configurations.
Then, choose which .dart
file to build: flutter run -t lib/main_debug.dart
import 'package:flutter/foundation.dart';
And use following const values:
if (kReleaseMode) {
// App is running in release mode.
} else if (kProfileMode) {
// App is running in profile mode.
} else if (kDebugMode) {
// App is running in debug mode.
} else if (kIsWeb) {
// App is running on the web.
}