EnvEditor class

Line-based .env file editor for CLI install commands.

Provides safe, idempotent read/write operations for .env files without relying on any third-party .env parser. Each method operates line-by-line to preserve user comments, blank lines, and key ordering.

Quoting rule: if a value contains spaces, #, ", ', or $, the value is wrapped in double quotes and any inner double quotes are backslash-escaped. All other values are emitted bare.

Usage

// Write (or update) a key.
EnvEditor.setKey('/app/.env', 'APP_ENV', 'production');

// Write with an explanatory comment above the line.
EnvEditor.setKey('/app/.env', 'APP_KEY', 'base64:abc...', comment: 'Generated by key:generate');

// Read the current value.
final env = EnvEditor.getKey('/app/.env', 'APP_ENV'); // 'production'

// Check presence.
if (!EnvEditor.hasKey('/app/.env', 'APP_DEBUG')) {
  EnvEditor.setKey('/app/.env', 'APP_DEBUG', 'false');
}

// Remove a key (and its leading comment).
EnvEditor.removeKey('/app/.env', 'LEGACY_KEY');

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

getKey(String envPath, String key) String?
Read the current raw value for key from the .env file at envPath.
hasKey(String envPath, String key) bool
Return true when key exists in the .env file at envPath.
removeKey(String envPath, String key) → void
Delete the key line (and the comment immediately above it, if any) from the .env file at envPath.
setKey(String envPath, String key, String value, {String? comment}) → void
Set or update key to value in the .env file at envPath.