Sub topic 3
Wrapping settings in an AppConfig class
This chapter assumes basic Dart and Flutter development, that you can run flutter run, and that you know what an API base URL is. The previous chapter introduced separate environments. Here, you will turn environment values into one typed object that the rest of the app can use.
Why use an AppConfig class?
An environment can have several settings:
- An API base URL
- A logging switch
- A feature switch
- A timeout value
- A service identifier
Passing each value separately makes constructors and function calls harder to read. A configuration class groups related settings and gives each setting a clear type.
AppConfig is a typed object. Its fields say exactly what kind of values the app expects.
| Setting | Dart type | Example |
|---|---|---|
| API base URL | `String` | `https://api.dev.example.com` |
| Logging enabled | `bool` | `true` |
| Request timeout | `int` | `30` |
Read compile-time values
Flutter's --dart-define option supplies values while the app is compiled. Dart reads these values with String.fromEnvironment, bool.fromEnvironment, and similar constructors.
These constructors must be used in a constant context. A constant value is fixed during compilation and cannot change while the app runs.
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'),
);
}
void main() {
const config = AppConfig.fromEnvironment;
print('API: ${config.apiBaseUrl}');
print('Logging: ${config.enableLogging}');
}The const keywords are important. The constructor is constant, the environment values are read in a constant context, and the static configuration object is also constant.
Run the program with values for a development environment:
flutter run \
--dart-define=API_BASE_URL=https://api.dev.example.com \
--dart-define=ENABLE_LOGGING=trueFor several values, a file is easier to maintain:
{
"API_BASE_URL": "https://api.dev.example.com",
"ENABLE_LOGGING": "true"
}Save this as config/dev.json, then run:
flutter run --dart-define-from-file=config/dev.jsonBoth command-line options produce compile-time values. They do not create a file that the running app reads.
Inject configuration into the app
Keep configuration near the application boundary. The entrypoint can create the configuration, then pass it to the root widget.
void main() {
const config = AppConfig.fromEnvironment;
runApp(MyApp(config: config));
}MyApp and its child widgets can receive the same object through constructors or a dependency-injection system. This avoids reading environment values throughout the codebase.
Use defaults carefully. A default API URL can make local development convenient, but a wrong default can send a production build to the wrong server. For required production settings, validate the values at startup and fail with a clear message.
Understand the configuration flow
A single AppConfig object also makes testing easier. A test can construct a configuration directly without changing command-line arguments:
const testConfig = AppConfig(
apiBaseUrl: 'https://api.test.example.com',
enableLogging: false,
);This separates application behavior from the environment that starts it.
Exercise
Why should a Flutter app pass one AppConfig object to its root widget instead of reading String.fromEnvironment in many widgets?
Show answer
One typed object keeps configuration in one place, makes dependencies visible, and lets tests provide a different configuration directly.