load method

Future<void> load({
  1. String fileName = '.env',
  2. Parser parser = const Parser(),
  3. Map<String, String> mergeWith = const {},
  4. bool isOptional = false,
})

Loads environment variables from the env file into a map Merge with any entries defined in mergeWith

Implementation

Future<void> load(
    {String fileName = '.env',
    Parser parser = const Parser(),
    Map<String, String> mergeWith = const {},
    bool isOptional = false}) async {
  clean();
  List<String> linesFromFile;
  try {
    linesFromFile = await _getEntriesFromFile(fileName);
  } on FileNotFoundError {
    if (isOptional) {
      linesFromFile = [];
    } else {
      rethrow;
    }
  }

  final linesFromMergeWith = mergeWith.entries
      .map((entry) => "${entry.key}=${entry.value}")
      .toList();
  final allLines = linesFromMergeWith..addAll(linesFromFile);
  final envEntries = parser.parse(allLines);
  _envMap.addAll(envEntries);
  _isInitialized = true;
}