Sub topic 6
Reading the flavor in Dart with `appFlavor`
This chapter assumes basic Dart and Flutter development, familiarity with flutter run, and a Flutter project that has at least one native flavor or scheme. It also assumes you know that --dart-define supplies application settings separately from the native flavor name.
The appFlavor constant tells Dart which native flavor Flutter selected. It comes from package:flutter/services.dart:
import 'package:flutter/services.dart';
void main() {
final flavor = appFlavor ?? 'none';
print('Running flavor: $flavor');
}When you run the app with --flavor dev, appFlavor contains "dev". When you run without --flavor, it is null. The example converts null to "none" before printing it.
How the value reaches Dart
The command-line tool passes the flavor to the native build system. Android receives the value as a product flavor, and iOS receives it through a scheme and its build configuration. Flutter then exposes the selected name through appFlavor.
The name must match across these layers. If Android defines dev but the command uses development, the build may fail because that native flavor does not exist. On iOS, the scheme and its configuration must be set up for the name you pass.
Use `appFlavor` for behavior, not secrets
A common use is choosing a visible environment label or selecting a service configuration:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final label = switch (appFlavor) {
'dev' => 'Development',
'staging' => 'Staging',
'prod' => 'Production',
_ => 'Unknown',
};
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Environment')),
body: Center(child: Text(label)),
),
);
}
}This code reads the flavor when the widget builds and displays a label. You can also use the value to select an AppConfig, log the active environment, or enable development-only tools.
| Situation | Value of `appFlavor` |
|---|---|
| `flutter run --flavor dev` | `"dev"` |
| `flutter build apk --flavor prod` | `"prod"` |
| `flutter run` without `--flavor` | `null` |
appFlavor is different from String.fromEnvironment. The former identifies the native flavor selected by --flavor. The latter reads a compile-time key supplied by --dart-define. You can use both:
flutter run --flavor dev --dart-define=API_BASE_URL=https://api.dev.example.comThe flavor tells the app which native identity is active. The define supplies a setting such as an API URL.
Common errors
- Testing with
flutter runonly: This does not test a named flavor. Include--flavorin local and CI commands. - Comparing the wrong spelling: Flavor names are strings. Keep names such as
dev,staging, andprodconsistent. - Assuming
appFlavorloads every setting: It only identifies the active flavor. API URLs and feature flags still needAppConfig,--dart-define, or native resources. - Using it as a secret: The flavor name and any behavior compiled into the application can be inspected.
Exercise
What does appFlavor contain when the app is started with flutter run --flavor staging, and what does it contain when started with flutter run?
Show answer
It contains "staging" in the first case and null in the second case.