getEnv function Null safety

dynamic getEnv(
  1. String key,
  2. {dynamic defaultValue}
)

Returns a value from the .env file the key must exist as a string value e.g. APP_NAME.

Returns a String|bool|null|dynamic depending on the value type.

Implementation

dynamic getEnv(String key, {dynamic defaultValue}) {
  if (!dotenv.env.containsKey(key) && defaultValue != null) {
    return defaultValue;
  }

  String? value = dotenv.env[key];

  if (value == 'null' || value == null) {
    return null;
  }

  if (value.toLowerCase() == 'true') {
    return true;
  }

  if (value.toLowerCase() == 'false') {
    return false;
  }

  return value.toString();
}