flutter_swr 0.4.0
flutter_swr: ^0.4.0 copied to clipboard
A Flutter Hooks port of SWR: cache-then-revalidate data fetching with dedup, retry, polling, and app-resume revalidation.
0.4.0 - 2026-09-30 #
- Added revalidate-on-reconnect (React SWR's
revalidateOnReconnect). Implement the newSwrConnectivityinterface (aStream<bool>of online status) with any package, such asconnectivity_plusorobserve_internet_connectivity, or useSwrConnectivity.fromStream. Then pass it asSwrConfig.connectivity. On an offline→online transition, every mounteduseSwranduseSwrInfinitekey revalidates. SetSwrConfig.revalidateOnReconnect(defaulttrue) tofalseto opt out. The package adds no new dependencies. - Example app: reconnect revalidation using
observe_internet_connectivity. - Added
useSwrInfinite<T>(getKey, {fetcher, options, config}), a port of React SWR'suseSWRInfinitefor paginated lists. It returns(SwrResponse<List<T>>, SwrInfinite<T>).SwrInfinitehassize,setSize(n)(completes with the new response), a boundmutatethat refetches every page, andisLoadingMore/isReachingEnd.SwrInfiniteOptionshasinitialSize,revalidateAll,revalidateFirstPage,persistSizeandparallel, with React's defaults. The list is cached as one entry, so dedup, retry, polling, app-resume revalidation,onSuccess/onErroranduseSwrMutationrace protection all apply to it. Each page is also cached under its own key. - Added
swrInfiniteKey(getKey), the key auseSwrInfinitelist is cached under, for the top-levelmutateanduseSwrMutation(React'sunstable_serialize). - Example app: a Users screen that demonstrates
useSwrInfinitewithpull_to_refreshagainst reqres.in.
0.3.0 - 2026-09-24 #
- Added
useSwrMutation<T, Arg>(fetcher, {key, options}), a port of React SWR'suseSWRMutationfor writes that run only when triggered. It returns(SwrMutationState<T>, SwrTrigger<T, Arg>). With akeyit supportsoptimisticData,populateCache/populateCacheWithandrollbackOnError, and it revalidates the key's mounteduseSwrreaders afterwards. Without a key it only tracks the call's state. Call it astrigger(arg), ortrigger()for no argument. Options are set on the hook. Iftriggeris called again before the first call finishes, only the latest call updates state.trigger.reset()returns the state to idle. - New public types:
SwrTrigger,SwrMutationOptions,SwrMutationState. - Behavior change:
useSwrnow discards the result of a fetch that overlaps auseSwrMutationon the same key, success or error, and clears onlyisValidating. The key is then revalidated after the mutation. This only applies while auseSwrMutationis running on the key.mutateis unchanged. - Example app: products can be renamed from the detail screen, which demonstrates optimistic update, rollback and race protection.
- Fixed
SwrConfig.onSuccess/onErrornever being called. They now fire after eachuseSwrfetch whose result is written to the cache, with(data or error, key). This includes polling, app-resume, andmutaterevalidations. Each fetch fires once, even when several hooks share it through dedup. A fetch whose result is discarded because auseSwrMutationoverlapped it fires neither callback. An exception thrown by a callback is not recorded as a fetch error.
0.2.0 - 2026-09-21 #
- Added
skipError,skipLoadingOnReload, andskipLoadingOnRefreshparameters toSwrResponse.when/maybeWhen/map/maybeMap, mirroring Riverpod'sAsyncValue.when.skipError(defaultfalse) routes a failed background revalidation to thedatabranch instead oferrorwhen stale data is still available, so a transient fetch failure doesn't hide already-cached data. - Fixed a "key has no fetcher" error thrown by app-resume revalidation when a controller for a key exists (e.g. from a persisted cache) before its fetcher is attached on first mount.
0.1.0 - 2026-09-17 #
Initial release.
useSwrhook: stale-while-revalidate data fetching with request deduplication and automatic revalidation.SwrProviderfor scoping a cache to a subtree, with a shared global cache by default.mutatefor optimistic/manual cache updates, including a top-levelmutatethat reaches every registeredSwrCache(not just the default).- Conditional fetching (skip the fetch by passing a
nullkey). revalidateOnFocussupport viaSwrConfig.