Sub topic 2
What the --flavor flag does
This chapter assumes you can run flutter run and flutter build, that you can open the android/ and ios/ folders in an editor, and that you know what a bundle ID is.
A flavor is a named build variant of your app. The --flavor flag passes a name to the native build tools. Flutter hands that name to Gradle on Android and to Xcode on iOS. Each flavor can then have its own app ID, app name, and icon.
The working model is simple. One command selects one name, and each platform looks up a matching configuration:
flutter run --flavor dev -t lib/main_dev.dartReading the flavor in Dart with appFlavor
The appFlavor constant in package:flutter/services.dart holds the value passed to --flavor. It is null when you build without the flag.
import 'package:flutter/services.dart';
void printFlavor() {
print('Running flavor: ${appFlavor ?? 'none'}');
}Android: productFlavors in Gradle
Android defines flavors in android/app/build.gradle.kts, inside the android { } block. Each flavor belongs to a dimension. The applicationIdSuffix lets dev and prod install side by side on one device.
flavorDimensions += "env"
productFlavors {
create("dev") {
dimension = "env"
applicationIdSuffix = ".dev"
resValue("string", "app_name", "MyApp Dev")
}
create("prod") {
dimension = "env"
resValue("string", "app_name", "MyApp")
}
}The resValue line sets the app_name string. AndroidManifest.xml reads it through android:label="@string/app_name".
iOS: schemes and build configurations
Xcode has no flavor concept, so Flutter maps flavors to two things. The first is a scheme with the flavor name, such as dev. The second is a build configuration for each build mode, named like Debug-dev or Release-prod. You create these in Xcode by duplicating the default configurations.
| Flutter command | Android variant | iOS scheme | iOS configuration |
|---|---|---|---|
| `flutter run --flavor dev` | devDebug | dev | Debug-dev |
| `flutter build ios --release --flavor prod` | prodRelease | prod | Release-prod |
A common error is a name mismatch. If the flag says Dev and the scheme says dev, the build fails because it cannot find the variant. Keep the names identical on every platform.
Exercise
Exercise
You run flutter run --flavor staging. Which Android variant and which iOS configuration does Flutter look for in debug mode?
Show answer
Android looks for stagingDebug, the product flavor staging combined with the debug build type. iOS uses the scheme staging and the configuration Debug-staging.