# update method

1. @override

V update(

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

})
override

Updates the value for the provided `key`.

Returns the new value associated with the key.

If the key is present, invokes update with the current value and stores the new value in the map.

If the key is not present and `ifAbsent` is provided, calls `ifAbsent` and adds the key with the returned value to the map.

If the key is not present, `ifAbsent` must be provided.

    final planetsFromSun = <int, String>{1: 'Mercury', 2: 'unknown',
      3: 'Earth'};
    // Update value for known key value 2.
    planetsFromSun.update(2, (value) => 'Venus');
    print(planetsFromSun); // {1: Mercury, 2: Venus, 3: Earth}

    final largestPlanets = <int, String>{1: 'Jupiter', 2: 'Saturn',
      3: 'Neptune'};
    // Key value 8 is missing from list, add it using [ifAbsent].
    largestPlanets.update(8, (value) => 'New', ifAbsent: () => 'Mercury');
    print(largestPlanets); // {1: Jupiter, 2: Saturn, 3: Neptune, 8: Mercury}

## Implementation

    @override
    V update(K key, V Function(V value) update, {V Function()? ifAbsent}) {
      if (containsKey(key)) {
        final oldValue = this[key] as V;
        final newValue = update(oldValue);
        this[key] = newValue;
        return newValue;
      } else {
        if (ifAbsent != null) {
          final newValue = ifAbsent();
          this[key] = newValue;
          return newValue;
        }
        throw InvalidArgumentException('$key not found and no ifAbsent function provided.');
      }
    }