get<T> method

T get<T>(
  1. String path,
  2. Type type, [
  3. T? defaultValue
])

Retrieve a configuration value by path and type, with optional coercion.

path The path to the configuration value, e.g. "database.host". type The expected type. defaultValue The default value to return if the path is not found.

Returns the configuration value coerced to the specified type, or the default value if not found.

Implementation

T get<T>(String path, Type type, [T? defaultValue]) {
  final value = _resolveValue(path, defaultValue);

  if (value.runtimeType == type) {
    return value as T;
  }

  if (_coercions.containsKey(type)) {
    try {
      return _coercions[type]!(value) as T;
    }
    catch (e) {
      throw ConfigurationException('Error during coercion to $type', e as Exception?);
    }
  } else {
    throw ConfigurationException('Unknown coercion to $type');
  }
}