loadContent static method

void loadContent(
  1. String content, {
  2. bool overwrite = true,
})

Parses and loads environment variables from a raw .env formatted content string.

Ignores blank lines and comments starting with #. Strips surrounding single (') or double (") quotes from values.

If overwrite is true (the default), existing keys are overwritten; otherwise, previously loaded values are preserved.

Example

BloomEnv.loadContent('PORT=8080\nAPI_KEY="secret_key"');

Implementation

static void loadContent(String content, {bool overwrite = true}) {
  final lines = content.split('\n');
  for (var line in lines) {
    line = line.trim();
    if (line.isEmpty || line.startsWith('#')) continue;

    final eqIdx = line.indexOf('=');
    if (eqIdx == -1) continue;

    final key = line.substring(0, eqIdx).trim();
    var value = line.substring(eqIdx + 1).trim();

    // Strip surrounding quotes
    if ((value.startsWith('"') && value.endsWith('"')) ||
        (value.startsWith("'") && value.endsWith("'"))) {
      if (value.length >= 2) {
        value = value.substring(1, value.length - 1);
      }
    }

    if (overwrite || !_env.containsKey(key)) {
      _env[key] = value;
    }
  }
}