load method

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

Loads environment variables from the env file into a map Merge with any entries defined in mergeWith overrideWithFiles is a list of other env files whose values will override values read from fileName

Implementation

Future<void> load({
  // The name of the env file asset to load
  // This file should be defined in your pubspec.yaml assets
  String fileName = '.env',
  // A optional list of other env files whose values will override values read from [fileName]
  List<String> overrideWithFiles = 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 file(s)
  bool isOptional = false,
  // An optional custom parser to use for parsing the env file
  Parser parser = const Parser(),
}) async {
  clean();
  List<String> linesFromFile;
  List<String> linesFromOverrides;
  try {
    linesFromFile = await _getEntriesFromFile(fileName);
    linesFromOverrides = await _getLinesFromOverride(overrideWithFiles);
  } on FileNotFoundError {
    if (!isOptional) rethrow;
    linesFromFile = [];
    linesFromOverrides = [];
  } on EmptyEnvFileError {
    if (!isOptional) rethrow;
    linesFromFile = [];
    linesFromOverrides = [];
  }

  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;
}