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 per trigger call. Every field is nullable and means "unset" when null; merge combines two option sets field by field (the other set's non-null values win, the same rule as SwrConfig.merge), and resolve additionally fills in the built-in defaults.
SwrMutationState<T>
The local, per-hook state of a useSwrMutation call.
SwrProvider
Scopes an SwrConfig to a widget subtree.
SwrResponse<T>
Immutable snapshot of a useSwr call's data-fetching state.
SwrRetryPolicy
Configures how executeWithRetry retries a failing fetch.
SwrTrigger<T, Arg>
Runs a useSwrMutation mutation: trigger() / trigger(arg).

Functions

bindMutate<T>(SwrController<T> controller, Future<T> effectiveFetcher(), SwrControllerRegistry registry) → SwrMutate<T>
Builds the SwrMutate bound to controller: effectiveFetcher is used to revalidate controller's own key (the same fetcher-resolution useSwr uses for its own revalidation), and registry is 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 every useSwr/mutate invocation.
executeWithRetry<T>(Future<T> fetcher(), SwrRetryPolicy policy, {void onAttemptFailed(Object error, int attempt)?}) → Future<T>
Runs fetcher, retrying on failure per policy until 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 key in 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 single useSwr call's bound mutate (which only ever touches the one cache it was resolved from), this walks every SwrControllerRegistry created so far (see allRegistries) 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.