Dart environment config with defaults
Dart
import 'dart:io';
class Config {
Config._(this.host, this.port, this.debug);
final String host;
final int port;
final bool debug;
factory Config.fromEnv([Map<String, String>? env]) {
final e = env ?? Platform.environment;
final port = int.tryParse(e['PORT'] ?? '8080');
if (port == null || port < 1 || port > 65535) {
throw FormatException('PORT must be 1-65535, got ${e['PORT']}');
}
final host = e['HOST'] ?? 'localhost';
if (host.isEmpty) throw StateError('HOST must not be empty');
return Config._(host, port, e['DEBUG'] == '1');
}
}