getOrCompute<T> static method

T getOrCompute<T>(
  1. String key,
  2. T compute(), {
  3. required double width,
  4. required double height,
  5. required String orientation,
})

Get cached value or compute and cache it

Implementation

static T getOrCompute<T>(
  String key,
  T Function() compute, {
  required double width,
  required double height,
  required String orientation,
}) {
  final cacheKey = _generateCacheKey(width, height, orientation);

  // Clear cache if device dimensions changed
  if (_lastCacheKey != null && _lastCacheKey != cacheKey) {
    clear();
  }
  _lastCacheKey = cacheKey;

  final fullKey = '${cacheKey}_$key';

  if (_cache.containsKey(fullKey)) {
    return _cache[fullKey] as T;
  }

  final value = compute();
  _cache[fullKey] = value;
  return value;
}