valueOrNull method

T? valueOrNull([
  1. bool showSuggestions = false
])

Returns the value of the environment variable as type T, or null if not found or invalid.

Unlike value, this method never throws an exception. It is useful when you want optional values without breaking execution.

If showSuggestions is true, it will print a suggestion message with similar keys if available.

Example

final optionalToken = Env<String>('OPTIONAL_TOKEN').valueOrNull();
if (optionalToken != null) {
  print('Token found: $optionalToken');
} else {
  print('No token set');
}

Implementation

T? valueOrNull([bool showSuggestions = false]) {
  try {
    return value();
  } catch (e) {
    if (showSuggestions) {
      System.out.println(_messageSuggestion);
    }
    return null;
  }
}