putIfAbsentAsync method

FutureOr<T> putIfAbsentAsync(
  1. Object? o,
  2. FutureOr<T> ifAbsent()
)

Implementation

FutureOr<T> putIfAbsentAsync(Object? o, FutureOr<T> Function() ifAbsent) {
  if (o == null) {
    return ifAbsent();
  }

  var prev = this[o];
  if (prev != null) return prev;

  var ret = ifAbsent();

  if (ret is Future<T>) {
    return this[o] = ret.then((r) {
      this[o] = r;
      return r;
    });
  } else {
    this[o] = ret;
    return ret;
  }
}