removeKey static method
Delete the key line (and the comment immediately above it, if any)
from the .env file at envPath.
Idempotent: no-op when the key is absent or the file does not exist.
@param envPath Path to the .env file.
@param key Environment variable name to remove.
Implementation
static void removeKey(String envPath, String key) {
final file = File(envPath);
if (!file.existsSync()) return;
final lines = file.readAsLinesSync();
final keyPrefix = '$key=';
final keyIndex = _findKeyIndex(lines, keyPrefix);
if (keyIndex == -1) return;
// Remove the comment line immediately above the key line, if present.
if (keyIndex > 0 && lines[keyIndex - 1].startsWith('#')) {
lines.removeAt(keyIndex - 1);
// After removal the key line shifted up by one.
lines.removeAt(keyIndex - 1);
} else {
lines.removeAt(keyIndex);
}
_writeLines(file, lines);
}