getBool static method

bool getBool(
  1. String key, {
  2. bool? defaultValue,
})

Retrieves and parses a boolean environment variable for key.

Case-insensitively interprets "true", "1", "yes" as true, and "false", "0", "no" as false. If absent or invalid, returns defaultValue if provided. If absent or invalid and defaultValue is null, throws a StateError.

final isProduction = BloomEnv.getBool('IS_PROD', defaultValue: false);

Implementation

static bool getBool(String key, {bool? defaultValue}) {
  if (_env.containsKey(key)) {
    final val = _env[key]!;
    final lower = val.toLowerCase();
    if (lower == 'true' || lower == '1' || lower == 'yes') return true;
    if (lower == 'false' || lower == '0' || lower == 'no') return false;
  }
  if (defaultValue != null) return defaultValue;
  throw StateError(
      'BloomEnv: Missing required boolean environment variable "$key".');
}