Env<T> class

A generic configuration property accessor for environment variables.

The Env<T> class provides a strongly-typed way to fetch environment variables at runtime. You can specify a key and an optional default value. If the environment variable is missing and no default is set, an InvalidArgumentException is thrown.

It can also be used as an annotation to inject environment variables into fields.

Example

void main() {
  // Fetch a required environment variable.
  final apiUrl = Env<String>('API_URL').value();

  // Fetch an environment variable with a default fallback.
  final timeout = Env<int>('TIMEOUT', defaultValue: 30).value();

  // Gracefully handle missing values without throwing.
  final optionalVar = Env<String>('OPTIONAL_VAR').valueOrNull();

  print('API URL: $apiUrl');
  print('Timeout: $timeout');
  print('Optional: $optionalVar');
}
Mixed-in types
Annotations
  • @Generic.new(Env)
  • @Target.new({TargetKind.field})

Constructors

Env(String _key, {T? defaultValue})
Creates a new Env accessor for a given environment variable.
const

Properties

defaultValue → T?
Optional default value to return if the variable is missing.
final
environment Environment
Returns the globally attached Environment.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

exists() bool
Checks if the environment variable exists in the loaded configuration.
get([bool showSuggestions = false]) String?
Returns the value of the environment variable as a String.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
value() → T
Returns the value of the environment variable as type T.
valueOrNull([bool showSuggestions = false]) → T?
Returns the value of the environment variable as type T, or null if not found or invalid.

Operators

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