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);
  } on FileNotFoundError {
    if (!isOptional) rethrow;
  } on EmptyEnvFileError {
    if (!isOptional) rethrow;
  }

  for (final overrideFile in overrideWithFiles) {
    try {
      final lines = await _getEntriesFromFile(overrideFile);
      linesFromOverrides.addAll(lines);
    } on FileNotFoundError {
      if (!isOptional) rethrow;
    } on EmptyEnvFileError {
      if (!isOptional) rethrow;
    }
  }

  _mergeAndStore(
    linesFromFile: linesFromFile,
    linesFromOverrides: linesFromOverrides,
    mergeWith: mergeWith,
    parser: parser,
  );
}