load method
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,
);
}