parseEnv function

Map<String, String> parseEnv(
  1. String input
)

Parses input as a .env file into a flat key → value map. export prefixes are stripped. .env files have no sections, but if [section] headers do appear, every section's entries are flattened into one map (later keys win) rather than discarded — so the result is never lossy.

Example:

parseEnv('export TOKEN="ab\\ncd"\nPORT=8080');
// {TOKEN: ab⏎cd, PORT: 8080}

Audited: 2026-06-12 11:26 EDT

Implementation

Map<String, String> parseEnv(String input) {
  final Map<String, Map<String, String>> sections = parseIni(input, allowExport: true);
  final Map<String, String> flat = <String, String>{};
  // Flatten every section (normally just the global one) so a stray header in a
  // .env file does not silently drop its keys.
  for (final Map<String, String> section in sections.values) {
    flat.addAll(section);
  }
  return flat;
}