Skip to content

Instantly share code, notes, and snippets.

@nrbnlulu
Last active March 24, 2026 08:04
Show Gist options
  • Select an option

  • Save nrbnlulu/bd5439dfda1538298167a52eea3f6936 to your computer and use it in GitHub Desktop.

Select an option

Save nrbnlulu/bd5439dfda1538298167a52eea3f6936 to your computer and use it in GitHub Desktop.
dart env
class Env {
Map<String, String> _env = {};
static final Env instance = Env._();
Env._() {
// read cwd .env
_env = Platform.environment.map((key, value) => MapEntry(key, value));
final file = File('.env');
// coverage:ignore-start
if (file.existsSync()) {
final lines = file.readAsLinesSync();
for (final line in lines) {
int index = line.indexOf("=");
if (index != -1) {
_env[line.substring(0, index).trim()] = line.substring(index + 1).trim();
}
}
}
// coverage:ignore-end
}
void set(String key, String value) {
_env[key] = value;
}
void setDefault<T>(String key, T value) {
_env.putIfAbsent(key, () => value.toString());
}
void setBool(String key, bool value) {
set(key, value.toString());
}
String getString(String key, {String defaultValue = ''}) {
return _env[key] ?? defaultValue;
}
bool getBool(String key, {bool defaultValue = false}) {
final found = _env[key];
if (found == null) {
return defaultValue;
}
return found.toLowerCase() == 'true';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment