Sub topic 1
Why one app needs several environments
This chapter explains why a single Flutter codebase usually has to run against several environments, and which settings change between them. It also introduces the three mechanisms the rest of the tutorial covers: --dart-define, separate entrypoints, and the --flavor flag.
Prerequisites
This chapter assumes you can:
- Build and run a Flutter app with
flutter runandflutter buildfrom the command line. - Find your way around the
android/andios/project folders, without needing to edit them yet. - Explain what an API base URL is (the root address that all requests to a backend start with).
- Explain what an application ID (Android) or bundle identifier (iOS) is. It is the unique string that identifies an app on a device and in the app stores.
No native Gradle or Xcode knowledge is needed here. Later chapters cover that.
What an environment is
An environment is a complete, separate copy of the system your app talks to, plus the app settings that point at it. Most teams run at least three:
- dev (development): a sandbox where developers can break things. Data is fake, and the backend may change daily.
- staging: a copy of production, used for final checks and for QA testers. It is stable enough to test against.
- prod (production): the real system, used by real customers.
The app binary is the same Dart code in all three. What changes is the configuration around it. If you hard-code the production URL and then test a new feature against it, you can create real orders, send real emails, or corrupt real data. Separate environments keep that risk away from customers.
Why a single build is not enough
You could change a constant in the source code before every build. This fails in practice:
- You will forget to change it back. A release build that points at the dev server is a common and embarrassing mistake.
- Builds are no longer reproducible. The same commit produces different apps depending on what someone edited locally.
- You cannot test two environments at once. A tester often needs the staging app and the prod app on the same phone to compare behavior.
- Code review cannot catch the problem, because the edit often never reaches version control.
The goal is to make the environment an explicit input to the build, chosen by a command-line flag or a file. The source code then stays identical for all environments.
Which settings differ between environments
Settings fall into two groups. The first group is visible to your Dart code. The second is visible to the operating system and to the user.
| Setting | Group | Example |
|---|---|---|
| API base URL | Dart | `https://api.dev.example.com` |
| Logging verbosity | Dart | Verbose in dev, off in prod |
| Feature switches | Dart | Experimental screens enabled only in dev |
| Analytics and crash reporting | Dart and services | Disabled or sent to a test project in dev |
| Backend project files (for example Firebase) | Native | A separate config file per environment |
| Application ID / bundle identifier | Native | `com.example.myapp.dev` |
| App name shown on the home screen | Native | "MyApp Dev" |
| App icon | Native | A banner or color tint marking non-prod |
| Signing keys | Native | Debug key for dev, release key for prod |
The Dart group can be handled with compile-time values and a configuration class. The native group needs platform support, and that support is what Flutter calls flavors.
The application ID row deserves attention. Android and iOS both refuse to install two apps with the same identifier side by side. A second install replaces the first. If dev and prod share an ID, a tester cannot keep both apps on one device. Giving each environment its own ID lets them coexist.
Three mechanisms, three jobs
Flutter gives you three tools. They solve different problems and are often combined.
--dart-definepasses key-value pairs into the Dart compiler. Your code reads them withString.fromEnvironment,bool.fromEnvironment, andint.fromEnvironment. This handles the Dart group of settings.- Entrypoints with
-tlet you choose which Dart file containsmain(). A file such aslib/main_dev.dartcan hard-code dev settings and then start the shared app. --flavortells the native build tools (Gradle on Android, Xcode on iOS) which variant of the native project to build. This handles the native group: application ID, name, icon, and service files.
Each tool changes a different layer of the build, which the diagram shows.
Notice that --dart-define and -t both end up in Dart code, while --flavor never touches Dart code directly. Flutter does expose the flavor name to Dart afterward through appFlavor, covered in a later chapter.
A first look at the commands
Here is what the pieces look like together. You do not need to understand every part yet. Later chapters build each one.
flutter run --flavor dev -t lib/main_dev.dart --dart-define-from-file=config/dev.json
flutter build apk --release --flavor prod -t lib/main_prod.dart --dart-define-from-file=config/prod.jsonRead each command from left to right:
--flavor devpicks the native variant.-t lib/main_dev.dartpicks the Dart entrypoint.--dart-define-from-file=config/dev.jsonloads compile-time constants from a JSON file.
A config file for the dev environment is plain JSON with one key per setting:
{
"API_BASE_URL": "https://api.dev.example.com",
"ENABLE_LOGGING": "true"
}Your Dart code reads those keys at compile time:
class AppConfig {
const AppConfig({
required this.apiBaseUrl,
required this.enableLogging,
});
final String apiBaseUrl;
final bool enableLogging;
static const AppConfig fromEnvironment = AppConfig(
apiBaseUrl: String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'https://api.example.com',
),
enableLogging: bool.fromEnvironment('ENABLE_LOGGING'),
);
}The calls to String.fromEnvironment and bool.fromEnvironment must appear in a const context. The values are fixed when the app is compiled and cannot change while it runs. This is the central trade-off of the whole approach, and the next section looks at it.
Compile-time versus runtime configuration
Everything in this tutorial is compile-time configuration: the values are decided when you build, and they are baked into the binary. There is another approach, runtime configuration, where the app fetches its settings from a server or a file after it starts.
| Question | Compile-time (this tutorial) | Runtime |
|---|---|---|
| When is the value chosen? | At build | At app start or later |
| Can one binary serve all environments? | No, one build per environment | Yes, if it can choose a source |
| Can you change a value without a new release? | No | Yes |
| Does the app need a network call first? | No | Often yes |
| Risk of a wrong build reaching users | Depends on build discipline | Depends on the config server |
Compile-time configuration is simple and has no startup dependency. Its cost is that every environment is a separate build artifact, and a value change needs a new build. For most apps this is the right default, and runtime configuration is added only for settings that must change without a release, such as remote feature toggles.
Edge cases and common errors
- Missing defines fail silently. If you forget
--dart-define,String.fromEnvironmentreturns itsdefaultValue, or an empty string when none is given. The app builds and runs, but against the wrong settings. Choose defaults deliberately. A safe default points at dev, not prod. - Booleans arrive as strings. On the command line,
ENABLE_LOGGING=trueis the text "true".bool.fromEnvironmentconverts it, but a typo such asTrueoryesgivesfalse. - Hot reload does not pick up new defines. Compile-time constants are fixed at build. After you change a
--dart-definevalue or the JSON file, stop the app and run it again. - Flavor and defines can disagree. Nothing forces
--flavor prodto be paired withprod.json. Mixing them gives a prod-ID app that talks to the dev server. Later chapters show how to keep the pairs together with launch configurations and CI scripts. - A flavor needs native setup first. Running
flutter run --flavor devin a project with no flavor defined fails during the native build, because Gradle or Xcode cannot find a variant calleddev.
What the rest of the tutorial builds
The plan for the remaining chapters follows the layers shown above. You will first work on the Dart side: --dart-define, a typed AppConfig class, and entrypoints. Then you will move to the native side: the --flavor flag, Android product flavors, iOS schemes, and per-flavor names, IDs, icons, and service files. The last chapters cover the limits for secrets, running flavors from an IDE and CI, signing and releasing, and testing against different configurations.
Exercise
Your team has a dev API at https://api.dev.example.com and a prod API at https://api.example.com. A tester wants the dev and prod versions installed on the same phone at the same time. Which of the two layers (Dart-visible settings or native settings) must change to make that possible, and which setting in particular?
Show answer
The native layer must change. Android and iOS identify an app by its application ID or bundle identifier, and two apps with the same identifier cannot be installed side by side. Each flavor needs a different identifier, for example com.example.myapp.dev for dev. Changing only the API URL with --dart-define would not help, because the second install would replace the first.
Exercise
A release build of your app shows the verbose logging that you only wanted in dev. You built it with flutter build apk --release --flavor prod -t lib/main_prod.dart and did not pass any --dart-define flags. Given that ENABLE_LOGGING is read with bool.fromEnvironment('ENABLE_LOGGING'), what value does the app see, and what is the likely cause?
Show answer
With no define and no defaultValue, bool.fromEnvironment returns false, so logging is off from that source. Verbose logging in this build therefore comes from somewhere else, for example a defaultValue: true set in the code, a lib/main_prod.dart that hard-codes logging on, or a debug-only logging path that does not read the define. The lesson is that a missing define does not cause an error, so check each source of the setting.