ApiCache class
A local copy of the answers an app reads from its API, so a screen can draw what it showed last time while it asks the server again ("local then server").
Wrap each read once, where the API is called:
final cache = ApiCache(
store: ApiCacheStore.persistent(),
identity: () async => auth.currentUser?.uid,
);
CachedCall<Map<String, Object?>> profile(String id) =>
cache.json('profile:$id', () => api.get('/profiles/$id'));
The caller then picks the strategy each time (CachedCall.fetch,
CachedCall.values, CachedCall.server, or ViewModel.watch on a
screen). Every server answer is saved, whichever strategy asked for it.
Whose answers. Before anything is served or saved the cache asks
identity who is signed in — typically a user id, with anything else
that changes what the API returns, such as roles. When that differs from
the identity the saved answers belong to, they are all dropped first. A
null identity means nobody is signed in: everything is dropped, nothing
is served locally and nothing is saved. The owner is kept in the store,
so this holds across restarts too. followIdentity makes a sign-out drop
the answers at once instead of at the next read.
Constructors
-
ApiCache({ApiCacheStore? store, required Future<
String?> identity(), Duration maxAge = const Duration(days: 30)})
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- maxAge → Duration
-
How long a saved answer may go without being refreshed before it is
dropped. Checked once, the first time the cache is used.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call<
T> (String key, Future< T> fetch(), {required ApiCacheCodec<T> codec}) → CachedCall<T> -
Wraps a read whose answer
codecturns into text and back. -
clear(
) → Future< void> - Drops every saved answer, and keeps none that is still on its way. Call it after a write, so no screen draws a copy the write has made wrong.
-
dispose(
) → void - Stops following identity changes.
-
followIdentity(
Stream< Object?> changes) → void -
Checks
identityagain on every event ofchanges— an auth state or token stream — so the saved answers are dropped as soon as someone signs out or a different identity signs in. Replaces any stream followed before. -
json<
T> (String key, Future< T> fetch()) → CachedCall<T> -
Wraps a read whose answer is already JSON-shaped: maps, lists, strings,
numbers, booleans and null, such as a decoded response body.
Tis whatjsonDecodereturns at the top, typicallyMap<String, Object?>orList<Object?>. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
text(
String key, Future< String> fetch()) → CachedCall<String> - Wraps a read whose answer is text already, such as a raw response body. Saved exactly as received; turn it into models with CachedCall.map.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited