loadFromString method

void loadFromString({
  1. String envString = '',
  2. List<String> overrideWith = const [],
  3. Map<String, String> mergeWith = const {},
  4. bool isOptional = false,
  5. Parser parser = const Parser(),
})

Implementation

void loadFromString({
  // The env string to load
  String envString = '',
  // A optional list of other env strings whose values will override values read from [envString]
  List<String> overrideWith = const [],
  // A map of key-value pairs to merge with the loaded env variables
  Map<String, String> mergeWith = const {},
  // Whether to ignore not found and empty file errors when loading the env string
  bool isOptional = false,
  // An optional custom parser to use for parsing the env string
  Parser parser = const Parser(),
}) {
  clean();
  if (envString.isEmpty && !isOptional) {
    throw EmptyEnvFileError();
  }
  final linesFromFile = envString.split('\n');
  final linesFromOverrides = overrideWith
      .map((String lines) => lines.split('\n'))
      .expand((x) => x)
      .toList();
  final linesFromMergeWith = mergeWith.entries
      .map((entry) => "${entry.key}=${entry.value}")
      .toList();

  final allLines = linesFromMergeWith
    ..addAll(linesFromOverrides)
    ..addAll(linesFromFile);

  final envEntries = parser.parse(allLines);

  _envMap.addAll(envEntries);
  _isInitialized = true;
}