addIfNotNull method

Map<K, V>? addIfNotNull(
  1. K key,
  2. V value
)

Adds an item into the map if the value is not null and the key does not exist.

Example:

Map<String, int>? map = {"a": 1};
map = map.addIfNotNull("b", 2);
print(map); // {a: 1, b: 2}

map = map.addIfNotNull("b", null);
print(map); // {a: 1, b: 2} (unchanged)

Implementation

Map<K, V>? addIfNotNull(K key, V value) {
  if (value != null) this?.putIfAbsent(key, () => value);
  return this;
}