set method
Sets a configuration value using dot notation.
Creates nested objects as needed. Useful for runtime configuration overrides or dynamic settings.
config.set('app.api_key', 'secret-key');
config.set('cache.enabled', true);
config.set('database.connections.max', 10);
Throws ConfigException if trying to set a nested property on a non-object value.
Implementation
@override
void set(String key, dynamic value) {
final parts = key.split('.');
final configName = parts.first;
_runtimeDirtyConfigs.add(configName);
if (!_config.containsKey(configName)) {
_config[configName] = <String, dynamic>{};
}
var current = _config[configName];
for (var i = 1; i < parts.length - 1; i++) {
if (current is Map<String, dynamic>) {
if (!current.containsKey(parts[i])) {
current[parts[i]] = <String, dynamic>{};
}
current = current[parts[i]];
} else {
throw ConfigException('Cannot set nested property on non-object value');
}
}
if (current is Map<String, dynamic>) {
current[parts.last] = value;
_cacheTimestamps[configName] = DateTime.now();
} else {
throw ConfigException('Cannot set property on non-object value');
}
}