env<T extends Object> function

T env<T extends Object>(
  1. String name,
  2. T defaultValue
)

Implementation

T env<T extends Object>(String name, T defaultValue) {
  _env ??= DotEnv(quiet: true, includePlatformEnvironment: true)..load();
  final strVal = _env![name];
  if (strVal == null) return defaultValue;

  final parsedVal = switch (T) {
    const (String) => strVal,
    const (int) => int.parse(strVal),
    const (num) => num.parse(strVal),
    const (bool) => bool.parse(strVal),
    const (double) => double.parse(strVal),
    const (List<String>) => jsonDecode(strVal),
    _ => throw ArgumentError.value(
        T, null, 'Unsupported Type used in `env` call.'),
  };
  return parsedVal as T;
}