update method

V update(
  1. K key,
  2. V update(
    1. V
    ), {
  3. V ifAbsent()?,
})

Updates the value associated with the specified key using the provided update function.

If the key is not found, the ifAbsent function is called to provide a default value. If the ifAbsent function is not provided, an ArgumentError is thrown.

Returns the updated value.

@ai Use this method to update the value associated with a key in the map.

Implementation

V update(
  final K key,
  final V Function(V) update, {
  final V Function()? ifAbsent,
}) {
  final items = {..._items};
  final value = items[key];
  V? updatedValue;
  if (value != null) {
    updatedValue = update(value);
  } else if (ifAbsent != null) {
    updatedValue = ifAbsent();
  }
  if (updatedValue != null) {
    upsert(updatedValue, key: key);
  } else {
    throw ArgumentError.value(key, 'value', 'Value not provided');
  }
  return updatedValue;
}