putIfNotNull method

bool putIfNotNull(
  1. K? key,
  2. V? value
)

Associates the key with the given value, and returns true, if neither is null.

Replaces an entry if it already exists.

<String, int>{}.putIfNotNull('a', 1); // true, {'a': 1}

<String, int>{'a': 0}.putIfNotNull('a', 1); // true, {'a': 1}

<String, int>{}.putIfNotNull('a', null); // false, {}

<String, int>{}.putIfNotNull(null, 1); // false, {}

Implementation

bool putIfNotNull(K? key, V? value) {
  final result = key != null && value != null;
  if (result) {
    this[key] = value;
  }

  return result;
}