value method

T value()

Returns the value of the environment variable as type T.

  • If the variable is found in the current application context, it is returned.
  • If not found but a defaultValue is set, the default is returned.
  • If neither is available, an InvalidArgumentException is thrown.

Example

final port = Env<int>('PORT', defaultValue: 8080).value();
print('Server running on port: $port');

Implementation

T value() {
  T? value = environment.getPropertyAs<T>(_key, Class<T>(), defaultValue);

  if (value.isNotNull) {
    return value!;
  }

  if (defaultValue.isNotNull) {
    return defaultValue!;
  }

  throw InvalidArgumentException(_messageSuggestion);
}