Sub topic 2
Passing configuration with `--dart-define`
Assume that you know basic Dart and Flutter development, can run flutter run and flutter build, and understand API base URLs. This chapter focuses on compile-time configuration: values supplied when Flutter builds or runs the app.
Why use `--dart-define`
An app usually needs different settings for development, testing, and production. For example, each environment might use a different API server or logging policy.
--dart-define passes a key-value pair to the Dart compiler:
flutter run \
--dart-define=API_BASE_URL=https://api.dev.example.com \
--dart-define=ENABLE_LOGGING=trueDart reads these values with String.fromEnvironment and bool.fromEnvironment. The values become part of the compiled application.
flowchart TD
A[flutter run or flutter build] --> B[--dart-define values]
B --> C[Dart compiler]
C --> D[String.fromEnvironment and bool.fromEnvironment]
D --> E[App configuration]The values are fixed for that build. Changing a value requires running or building the app again.
Store several values in a file
Long command lines are difficult to review and easy to mistype. Flutter also supports --dart-define-from-file. The file below contains a JSON object:
{
"API_BASE_URL": "https://api.dev.example.com",
"ENABLE_LOGGING": "true"
}Save it as config/dev.json, then use it like this:
flutter run --dart-define-from-file=config/dev.jsonYou can keep separate files for different environments:
| File | API server | Logging |
|---|---|---|
| `config/dev.json` | Development server | Enabled |
| `config/prod.json` | Production server | Disabled |
Keep these files consistent. A missing key does not produce a compiler error when you read it with a default value, so an accidental omission can be hard to notice.
Read values with environment constructors
Environment constructors must be called in a constant context. Use const when declaring the configuration:
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'),
);
}String.fromEnvironment returns the text associated with a key. The defaultValue is used when API_BASE_URL was not supplied.
bool.fromEnvironment returns true when the value is the string true. Otherwise, including when the key is absent, it returns false unless you provide another default:
static const enableLogging = bool.fromEnvironment(
'ENABLE_LOGGING',
defaultValue: false,
);Use exact key names. API_BASE_URL and api_base_url are different keys.
Use the configuration in the app
A widget or service can use the shared configuration:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
const MyApp({required this.config, super.key});
final AppConfig config;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(
'API: ${config.apiBaseUrl}\n'
'Logging: ${config.enableLogging}',
),
),
),
);
}
}A typed AppConfig keeps configuration access in one place. It also makes dependencies explicit instead of scattering calls to String.fromEnvironment throughout the application.
Common errors
- Calling
String.fromEnvironmentwithout aconstcontext. - Misspelling a key and silently receiving the default.
- Passing
truefor a string setting when the application expects a URL. - Assuming a value supplied to one build is available in a later build.
- Committing sensitive values because the configuration file is outside the Dart source.
The final error is a security issue. Anything compiled into an application can usually be extracted from the application package. Use compile-time defines for public settings such as endpoints, feature switches, and build labels. Do not use them for passwords, private API keys, or signing credentials.
Exercise
What value does bool.fromEnvironment('ENABLE_LOGGING') return when the build command does not provide ENABLE_LOGGING?
Show answer
It returns false by default. You can make the fallback explicit with defaultValue: false.