parse static method

Map<String, String> parse(
  1. String contents
)

Reads the text of a .env file.

  • KEY=value, one per line. Lines that start with # are comments.
  • KEY=value # note: a # after a space starts a comment.
  • KEY="a # b": quotes (", ' or `) keep the value as it is, also a #. The quotes are removed.
  • KEY=abc#def stays abc#def (no space before the #).

Implementation

static Map<String, String> parse(String contents) {
  Map<String, String> data = <String, String>{};
  for (String line in contents.split('\n')) {
    String text = line.trim();
    if (text.isEmpty || text.startsWith('#')) continue;
    int equal = text.indexOf('=');
    if (equal < 1) continue;
    data[text.substring(0, equal).trim()] = _value(text.substring(equal + 1));
  }
  return data;
}