flutter_swr library
Flutter Hooks for Data Fetching.
Classes
- AppLifecycleListener
- Revalidates every currently-subscribed key when the app resumes from the background.
-
CacheEntry<
T> - An immutable snapshot of cached state for a single key.
- InMemoryCache
- Default SwrCache implementation backed by an in-memory Map.
- SwrCache
- Storage interface for cached CacheEntry values, keyed by an arbitrary, already-normalized Object key.
- SwrConfig
- Immutable, partially-specified SWR configuration.
-
SwrMutationOptions<
T, Arg> -
Options for
useSwrMutation, settable at the hook level and overridable pertriggercall. Every field is nullable and means "unset" whennull; merge combines two option sets field by field (the other set's non-null values win, the same rule asSwrConfig.merge), and resolve additionally fills in the built-in defaults. -
SwrMutationState<
T> -
The local, per-hook state of a
useSwrMutationcall. - SwrProvider
- Scopes an SwrConfig to a widget subtree.
-
SwrResponse<
T> -
Immutable snapshot of a
useSwrcall's data-fetching state. - SwrRetryPolicy
- Configures how executeWithRetry retries a failing fetch.
-
SwrTrigger<
T, Arg> -
Runs a
useSwrMutationmutation:trigger()/trigger(arg).
Functions
-
bindMutate<
T> (SwrController< T> controller, Future<T> effectiveFetcher(), SwrControllerRegistry registry) → SwrMutate<T> -
Builds the SwrMutate bound to
controller:effectiveFetcheris used to revalidatecontroller's own key (the same fetcher-resolutionuseSwruses for its own revalidation), andregistryis used to reach other keys' controllers for cascade invalidation. -
ensureAppLifecycleListener(
SwrControllerRegistry registry) → AppLifecycleListener -
The AppLifecycleListener installed for
registry, creating one (and registering it with WidgetsBinding) on first use. Idempotent — safe to call on everyuseSwr/mutateinvocation. -
executeWithRetry<
T> (Future< T> fetcher(), SwrRetryPolicy policy, {void onAttemptFailed(Object error, int attempt)?}) → Future<T> -
Runs
fetcher, retrying on failure perpolicyuntil it succeeds, SwrRetryPolicy.maxAttempts is exhausted, or SwrRetryPolicy.shouldRetry declines a retry — whichever comes first. Rethrows the last error when retries are exhausted or declined. -
mutate<
T> (Object key, {T? data, bool revalidate = true}) → Future< void> -
Top-level mutate, reaching
keyin every SwrCache it's known to be cached in — the package-level default cache, and any custom cache supplied to a SwrProvider, scoped or not. Unlike a singleuseSwrcall's boundmutate(which only ever touches the one cache it was resolved from), this walks everySwrControllerRegistrycreated so far (seeallRegistries) so a delete or update elsewhere in the app can invalidate a key across independently-scoped subtrees in one call. -
normalizeKey(
Object key) → Object -
Normalizes a user-supplied cache key into an Object with correct
value-based
==/hashCode, so equivalent keys collide in the cache. -
useSwr<
T> (Object? key, {Future< T> fetcher()?, SwrConfig? config}) → (SwrResponse<T> , SwrMutate<T> ) -
Fetches (and caches, deduplicates, and keeps fresh) the data for
key. -
useSwrMutation<
T, Arg> (Future< T> fetcher(Arg arg), {Object? key, SwrMutationOptions<T, Arg> ? options}) → (SwrMutationState<T> , SwrTrigger<T, Arg> ) -
Runs a remote mutation on demand and tracks its state locally — a Dart
port of React SWR's
useSWRMutation.
Typedefs
-
SwrMutate<
T> = Future< void> Function({T? data, List<Object> invalidate, T updater(T?)?}) -
Bound mutate function returned by
useSwr, scoped to the hook's own key:mutate(data: newValue)/mutate(updater: (prev) => next)writes directly to the cache (bypassing dedup — a direct write isn't a fetch), then revalidates;mutate()with neither just revalidates.