SwrResponse<T> class

Immutable snapshot of a useSwr call's data-fetching state.

A single concrete class (rather than a sealed-class hierarchy) is enough here: there's no meaningfully different shape of data per state, just different field values, so when/maybeWhen/map/ maybeMap are straightforward conditionals over data/error/isLoading.

Constructors

SwrResponse({T? data, Object? error, StackTrace? stackTrace, required bool isLoading, required bool isValidating})
const

Properties

data → T?
The last successfully fetched value, if any.
final
error → Object?
The error from the most recent failed fetch, if any. Cleared on the next successful fetch, but preserved (alongside the last-good data) across a failed one.
final
hashCode → int
The hash code for this object.
no setterinherited
isLoading → bool
True only until the first fetch for this key has ever resolved (successfully or not) — i.e. there's no data and no error yet.
final
isValidating → bool
True while a fetch (initial or background revalidation) is in flight, regardless of whether data is already populated.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
stackTrace → StackTrace?
The stack trace captured alongside error, if any. null whenever error is null.
final

Methods

map<R>({required R data(SwrResponse<T> response), required R error(SwrResponse<T> response), required R loading(SwrResponse<T> response), bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false}) → R
Like when, but each callback receives the whole SwrResponse — e.g. to inspect isValidating alongside data — instead of just the raw value. See when for why the idle useSwr(null) response lands in loading rather than data, and for skipError/ skipLoadingOnReload/skipLoadingOnRefresh.
maybeMap<R>({R data(SwrResponse<T> response)?, R error(SwrResponse<T> response)?, R loading(SwrResponse<T> response)?, bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, required R orElse()}) → R
Like map, but any state without a matching callback falls back to orElse instead of requiring every branch.
maybeWhen<R>({R data(T data)?, R error(Object error, StackTrace stackTrace)?, R loading()?, bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, required R orElse()}) → R
Like when, but any state without a matching callback falls back to orElse instead of requiring every branch.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
when<R>({required R data(T data), required R error(Object error, StackTrace stackTrace), required R loading(), bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false}) → R
Maps the current state to R using raw data/error values.

Operators

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