getAndRemove method Null safety

  1. @override
Future<T?> getAndRemove(
  1. String key
)

Removes the entry for a key only if currently mapped to some value.

  • key: key with which the specified value is associated

Returns the value if exists or null if no mapping existed for this key

Implementation

@override
Future<T?> getAndRemove(String key) {
  // #region Statistics
  Stopwatch? watch;
  Future<T?> Function(T? value) posGetRemove =
      (T? value) => Future.value(value);
  if (statsEnabled) {
    watch = clock.stopwatch()..start();
    posGetRemove = (T? value) {
      if (value == null) {
        stats.increaseMisses();
      } else {
        stats.increaseGets();
      }
      stats.increaseRemovals();
      if (watch != null) {
        int elapsed = watch.elapsedMicroseconds;
        stats.addGetTime(elapsed);
        stats.addRemoveTime(elapsed);
        watch.stop();
      }

      return Future.value(value);
    };
  }
  // #endregion

  return _primary.getAndRemove(key).then((value) {
    if (value == null) {
      return _secondary.getAndRemove(key);
    }

    return _secondary.remove(key).then((_) => value);
  }).then(posGetRemove);
}