loadFromFile method
Loads environment variables from a file.
Supports standard .env file format with the following features:
- Comments (lines starting with #)
- Export statements (
export KEY=value) - Quoted values (single and double quotes)
- Variable substitution (
$VARor${VAR}) - Escaped characters
If the file doesn't exist, this method returns silently.
env.loadFromFile('.env.local');
env.loadFromFile('config/production.env');
Implementation
@override
void loadFromFile(String path) {
final file = File(path);
if (!file.existsSync()) {
return;
}
_loadedFiles.add(path);
for (final line in file.readAsLinesSync()) {
final trimmedLine = line.trim();
// Skip empty lines and comments
if (trimmedLine.isEmpty || trimmedLine.startsWith('#')) {
continue;
}
// Handle export statements
if (trimmedLine.startsWith('export ')) {
final exportLine = trimmedLine.substring(7).trim();
_parseEnvLine(exportLine);
continue;
}
_parseEnvLine(trimmedLine);
}
}