tryPutIfAbsent method

RxResult<V> tryPutIfAbsent(
  1. K key,
  2. V ifAbsent()
)

Put if absent with error handling

Implementation

RxResult<V> tryPutIfAbsent(K key, V Function() ifAbsent) {
  return RxResult.tryExecute(() {
    final newMap = Map<K, V>.from(value);
    final sizeBefore = newMap.length;
    final result = newMap.putIfAbsent(key, ifAbsent);
    if (sizeBefore != newMap.length) value = newMap;
    return result;
  }, 'put if absent in map');
}