get<T> method
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');
}
}