withDependency<DependencyType> method

DependentCachedValue<CacheContentType, DependencyType> withDependency<DependencyType>(
  1. ComputeCacheDependency<DependencyType> on
)

Wraps the declared CachedValue with a DependentCachedValue.

The dependency callback on is called on every value access. So it is recommended to keep the dependency callback as declarative as possible.

Usage example:

int factorial(int n) {
  if (n < 0) throw ('Negative numbers are not allowed.');
  return n <= 1 ? 1 : n * factorial(n - 1);
}

int originalValue = 1;
final factorialCache = CachedValue(() => factorial(originalValue))
.withDependency(
  () => originalValue,
);
print(factorialCache.value); // 1

originalValue = 6;
print(factorialCache.value); // 720

Implementation

DependentCachedValue<CacheContentType, DependencyType>
    withDependency<DependencyType>(
  ComputeCacheDependency<DependencyType> on,
) =>
        DependentCachedValue._(this, on);