parse static method
Reads the text of a .env file.
KEY=value, one per line. Lines that start with#are comments.export KEY=valueis read likeKEY=value.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#defstaysabc#defandKEY=#fffstays#fff(no space before the#).- One line is one value. A quoted value can not go over more lines.
With
expand, write a new line as\ninside"quotes.
With expand (CoreFlags.envExpand):
${OTHER}is the value ofOTHER, in an unquoted or a"value. A real environment variable wins, then a key above this line (or inknown). An unknown name stays as it is:${OTHER}.- In a
"value,\n,\t,\",\\and\$become a new line, a tab,",\and$. A'or`value stays raw.
Implementation
static Map<String, String> parse(
String contents, {
Map<String, String> known = const <String, String>{},
bool expand = false,
}) {
Map<String, String> data = <String, String>{};
String? lookup(String name) =>
Platform.environment[name] ?? data[name] ?? known[name];
for (String line in contents.split('\n')) {
String text = line.trim();
if (text.isEmpty || text.startsWith('#')) continue;
text = text.replaceFirst(_export, '');
int equal = text.indexOf('=');
if (equal < 1) continue;
data[text.substring(0, equal).trim()] = _value(
text.substring(equal + 1),
expand ? lookup : null,
);
}
return data;
}