put method
void
put(
- K key,
- V value
Associates value with key, replacing any previous value.
Cancels the previous finalization and attaches a new one.
When the key is GC-collected, onFinalize is called with the value.
Implementation
void put(K key, V value) {
final prevBox = _expando[key];
if (prevBox != null) {
if (identical(prevBox.value, value)) {
// Same object already associated → avoid detach/attach churn.
return;
}
// Cancel pending finalization for the previous value because the
// association is being replaced.
_finalizer.detach(prevBox);
}
final box = _ExpandoBox<V>(value);
// Weakly associate value with the key.
_expando[key] = box;
// Attach finalization to the key lifecycle.
// When key is GC-collected, `_onFinalize(box)` will be invoked.
// `detach: box` allows later manual cancellation via detach(box).
_finalizer.attach(key, box, detach: box);
}