putIfAbsent method Null safety

  1. @override
Future<bool> putIfAbsent(
  1. String key,
  2. T value,
  3. {CacheEntryDelegate<T>? delegate}

Associates the specified key with the given value if not already associated with a value.

  • key: key with which the specified value is to be associated
  • value: value to be associated with the specified key
  • delegate: provides the caller a way of changing the CacheEntry before persistence

Returns true if a value was set.


Future<bool> putIfAbsent(String key, T value,
    {CacheEntryDelegate<T>? delegate}) {
  // #region Statistics
  Stopwatch? watch;
  Future<bool> Function(bool ok) posPut = (bool ok) => Future<bool>.value(ok);
  if (statsEnabled) {
    watch = clock.stopwatch()..start();
    posPut = (bool ok) {
      if (ok) {
      if (watch != null) {

      return Future<bool>.value(ok);
  // #endregion

  return _secondary
      .putIfAbsent(key, value, delegate: delegate)
      .then((_) => _primary.putIfAbsent(key, value, delegate: delegate))