set method
Sets a value at the given dot-notation key.
Automatically creates intermediate nested maps as needed.
Throws ArgumentError if key is invalid.
Example:
config.set('cache.enabled', true);
config.set('features.login', false);
Implementation
void set(String key, dynamic value) {
if (!_isValidKey(key)) {
throw ArgumentError("Invalid key format: $key");
}
final segments = key.split('.');
dynamic currentItems = _items;
// Traverse and create nested maps up to the second-to-last segment
for (int i = 0; i < segments.length - 1; i++) {
final segment = segments[i];
if (!currentItems.containsKey(segment) || currentItems[segment] is! Map) {
currentItems[segment] = <String, dynamic>{};
}
currentItems = currentItems[segment];
}
// Set the final value
currentItems[segments.last] = value;
}