upsert method

void upsert({
  1. required TK key,
  2. TV value(
    1. TV item
    )?,
  3. required TV defaultValue,
  4. bool overwrite = true,
})

if key present, overwrites value, if overwriteValue set, else inserts

Implementation

void upsert({
  required TK key,
  TV Function(TV item)? value,
  required TV defaultValue,
  bool overwrite = true,
}) {
  if (!containsKey(key)) {
    this[key] = defaultValue;
  } else {
    if (!overwrite) {
      return;
    }
  }
  if (value != null && containsKey(key)) {
    this[key] = value(this[key] as TV);
  }
}