setValue method
Sets the value
for the key
on the registry. If the value
is null
,
this redirects to removeValue.
If the value
is different than the current value for the key
then this
will fire an event on the valueStream with the key
so listeners can be
notified that it has changed.
This accepts an optional originator
to allow widgets that set values to
know when the stream fires if they are responding to their own event or
not.
Implementation
void setValue(
String key,
dynamic value, {
String? originator,
}) {
assert(key.isNotEmpty == true);
if (value == null) {
removeValue(
key,
originator: originator,
);
} else {
var current = _values[key];
var equals = current == value;
if (current is List || current is Set || current is Map) {
equals = DeepCollectionEquality().equals(current, value);
}
if (!equals) {
_logger.finest(
'[setValue]: [$key] = [${value?.toString().substring(0, min(80, value?.toString().length ?? 0))}]',
);
_values[key] = value;
_valueStreamController?.add(WidgetValueChanged(
id: key,
originator: originator,
value: value,
));
}
}
}