Sub topic 5
What the `--flavor` flag does
Assume you can build and run a Flutter app, understand basic Dart, and have seen the android/ and ios/ project folders. This chapter focuses on the --flavor flag. A flavor is a named native build variant, such as dev, staging, or prod.
The flag selects a native variant when Flutter invokes Android Gradle or an iOS scheme. It does not automatically change Dart settings such as an API URL. Combine it with --dart-define, a separate entrypoint, or both when the app needs environment-specific behavior.
What the flag selects
This command asks Flutter to run the dev variant:
flutter run --flavor devThe name after --flavor must match a native configuration in the platform project. On Android, it normally matches a productFlavors entry in Gradle. On iOS, it normally matches a scheme or a related build configuration.
The selected flavor can control native properties:
| Property | Example for `dev` | Why it matters |
|---|---|---|
| Application ID | `com.example.myapp.dev` | Installs beside the production app |
| Display name | `MyApp Dev` | Makes the environment visible |
| App icon | A development icon | Reduces accidental testing in production |
| Service file | Development Firebase configuration | Connects to the correct backend |
The flavor name is separate from the application ID. A flavor called dev does not create .dev in the ID unless the native configuration adds that suffix.
Flavor versus Dart configuration
These mechanisms operate at different layers:
--flavor devselects native build settings.--dart-define=KEY=valuesupplies compile-time Dart values.-t lib/main_dev.dartselects a Dart entrypoint.
A typical development command combines them:
flutter run \
--flavor dev \
-t lib/main_dev.dart \
--dart-define=API_BASE_URL=https://api.dev.example.comThis command selects the native dev variant, starts the main_dev.dart Dart file, and supplies an API URL to Dart code. Leaving out one part can produce an incomplete setup. For example, a development application ID with a production API URL may still connect to production data.
Reading the active flavor
Flutter exposes the selected flavor to Dart through appFlavor:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(Text('Flavor: ${appFlavor ?? 'none'}'));
}appFlavor is dev when the app was launched with --flavor dev. It is null when no flavor was selected. Treat this value as information about the native build. It should not replace a typed application configuration object for URLs, feature switches, or other Dart settings.
Common errors
- “Flavor not found”: the name does not match an Android product flavor or iOS scheme.
- The app still uses production data:
--flavorchanged native settings, but the Dart API configuration was not changed. - The wrong app starts: the IDE launch configuration may omit
--flavor. - Two apps cannot install together: the variants use the same application ID. Add a development suffix on Android and a distinct bundle identifier on iOS.
Exercise
What does --flavor dev change, and what might you still need to configure separately?
Show answer
It selects the native build variant named dev. You may still need --dart-define values or a different Dart entrypoint to select the correct Dart behavior and backend.