Sub topic 5
Passing Environment Values with `--dart-define`
This chapter assumes you know basic Dart and Flutter, can run flutter commands in a terminal, and understand that a Flutter app can have different build configurations. You will learn how to pass compile-time values into an app without changing its source code.
Why use `--dart-define`?
An environment value is a setting that changes between builds. Common examples include:
- The base URL of an API.
- A feature flag that enables unfinished functionality.
- The name of a backend environment.
- A logging mode.
Flutter passes these values to Dart with the --dart-define option:
flutter run --dart-define=API_BASE_URL=https://staging.example.comThe value is available while Flutter compiles the app. It is not read from the device's operating-system environment while the app is running.
Dart reads the value with String.fromEnvironment, bool.fromEnvironment, or int.fromEnvironment. These constructors must normally be used with const, because the values are compile-time constants.
import 'package:flutter/material.dart';
const apiBaseUrl = String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'https://staging.example.com',
);
const enableNewCheckout = bool.fromEnvironment(
'ENABLE_NEW_CHECKOUT',
defaultValue: false,
);
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Environment example')),
body: Center(
child: Text(
'API: $apiBaseUrl\n'
'New checkout: $enableNewCheckout',
textAlign: TextAlign.center,
),
),
),
);
}
}The defaultValue is used when the command does not provide a value. A default makes local development easier, but choose it carefully. An accidental default could send a release build to the wrong backend.
Use different values for each build
You can provide several definitions in one command. Boolean values must be written as strings such as true or false.
flutter run \
--dart-define=API_BASE_URL=https://staging.example.com \
--dart-define=ENABLE_NEW_CHECKOUT=trueFor a release build, pass the production values explicitly:
flutter build appbundle --release \
--dart-define=API_BASE_URL=https://api.example.com \
--dart-define=ENABLE_NEW_CHECKOUT=falseThe value becomes part of the compiled application. Changing the command requires a new build. Restarting the app or changing a server setting cannot change a String.fromEnvironment value already compiled into the binary.
Protect secrets
--dart-define is configuration, not secret storage. API keys, passwords, and private tokens can be extracted from an application binary or observed while the app communicates with a service. Keep real secrets on a trusted server. The app should receive only the limited public configuration it needs.
For repeatable builds, store commands in a script or CI configuration. Keep secret values in the CI system's protected variables, but do not assume that passing them to Flutter makes them secret inside the app.
Check the value you built
A useful diagnostic screen can display the selected backend in debug or internal builds. Avoid displaying sensitive values in production logs. Also check the built app's behavior on a real device, because a correct command does not prove that the configured server is reachable or supports the release.
Exercise
What happens if API_BASE_URL is omitted from the build command and the code uses defaultValue: 'https://staging.example.com'?
Show answer
The compiled app uses https://staging.example.com. The value is selected during compilation, so changing a server setting later does not change it.