readEnvFile static method
Reads the environment file and returns a map of key-value pairs
Implementation
static Future<Map<String, String>> readEnvFile() async {
try {
final file = File(_envFilePath);
if (!await file.exists()) {
throw Exception('Environment file not found at $_envFilePath');
}
final contents = await file.readAsString();
final Map<String, String> envVars = {};
// Parse the file line by line
for (String line in contents.split('\n')) {
line = line.trim();
// Skip empty lines and comments
if (line.isEmpty || line.startsWith('#')) {
continue;
}
// Split by '=' and handle quoted values
final equalIndex = line.indexOf('=');
if (equalIndex > 0) {
final key = line.substring(0, equalIndex).trim();
String value = line.substring(equalIndex + 1).trim();
// Remove quotes if present
if ((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))) {
value = value.substring(1, value.length - 1);
}
envVars[key] = value;
}
}
return envVars;
} catch (e) {
throw Exception('Failed to read environment file: $e');
}
}