CachePolicy enum

Cache policy for API requests.

Defines how the networking layer should handle caching for requests.

Example:

await api.network<User>(
  request: (r) => r.get('/user'),
  cachePolicy: CachePolicy.cacheFirst,
  cacheKey: 'user_profile',
  cacheDuration: Duration(hours: 1),
);
Inheritance
Available extensions

Values

networkOnly → const CachePolicy

Always fetch from network, ignore cache. This is the default behavior.

cacheFirst → const CachePolicy

Try cache first, fallback to network if cache is empty or expired. Best for data that doesn't change often.

networkFirst → const CachePolicy

Try network first, fallback to cache if network fails. Best for data that should be fresh when possible.

cacheOnly → const CachePolicy

Only use cache, never make network request. Throws error if cache is empty.

staleWhileRevalidate → const CachePolicy

Return cached data immediately (if available), then fetch from network and update the cache in the background. The callback will only receive the initial cached data. Best for UI that needs immediate response but should stay updated.

Properties

description String

Available on CachePolicy, provided by the CachePolicyExtension extension

Human-readable description of the policy.
no setter
hashCode int
The hash code for this object.
no setterinherited
index int
A numeric identifier for the enumerated value.
no setterinherited
name String

Available on Enum, provided by the EnumName extension

The name of the enum value.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shouldFallbackToCache bool

Available on CachePolicy, provided by the CachePolicyExtension extension

Whether this policy should fallback to cache on network failure.
no setter
shouldFallbackToNetwork bool

Available on CachePolicy, provided by the CachePolicyExtension extension

Whether this policy should fallback to network on cache miss.
no setter
shouldRevalidateInBackground bool

Available on CachePolicy, provided by the CachePolicyExtension extension

Whether this policy should revalidate in the background.
no setter
shouldTryCacheFirst bool

Available on CachePolicy, provided by the CachePolicyExtension extension

Whether this policy should try cache first.
no setter
shouldTryNetwork bool

Available on CachePolicy, provided by the CachePolicyExtension extension

Whether this policy should try network.
no setter

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

values → const List<CachePolicy>
A constant List of the values in this enum, in order of their declaration.