Async and Practical
Environment Map Lite
A real app reads Platform.environment. For a deterministic teaching example, treat env vars as a fixed Map<String, String> and decode them with ?? defaults plus simple comparisons. The pattern transfers directly to real environment maps.
Program
Play the program to parse a port and a debug flag from a fixed env map.
env_map_lite.dart
Replay: real traced execution (multi-file project)
void main() {
var env = {'APP_PORT': '8080', 'APP_DEBUG': 'true'};
var port = int.parse(env['APP_PORT'] ?? '80');
var debug = env['APP_DEBUG'] == 'true';
var mode = debug ? 'debug' : 'release';
print('$port $mode');
}
env ← 2 keys
1void main() {2 var env = {'APP_PORT': '8080', 'APP_DEBUG': 'true'};3 var port = int.parse(env['APP_PORT'] ?? '80');values this step2 keysenvport ← 8080
2var env = {'APP_PORT': '8080', 'APP_DEBUG': 'true'};3var port = int.parse(env['APP_PORT'] ?? '80');4var debug = env['APP_DEBUG'] == 'true';values this step8080portdebug ← true
3var port = int.parse(env['APP_PORT'] ?? '80');4var debug = env['APP_DEBUG'] == 'true';5var mode = debug ? 'debug' : 'release';values this steptruedebugmode ← debug
4var debug = env['APP_DEBUG'] == 'true';5var mode = debug ? 'debug' : 'release';6print('$port $mode');values this stepdebugmodetruedebugprint('$port $mode');
5 var mode = debug ? 'debug' : 'release';6 print('$port $mode');7}output8080 debugvalues this step8080portdebugmode
env map
Treating env vars as a `Map<String, String>` keeps the example deterministic without `Platform.environment`.
safe parse
`env['APP_PORT'] ?? '80'` falls back to a default before `int.parse` runs.
string flag
Booleans usually arrive as strings; comparing `== 'true'` decodes them safely.