useQuery
const {
data,
error,
status,
isFetching,
isStale,
isError,
isSuccess,
failureCount,
failureReason,
refetch,
} = useQuery(
{
queryKey,
queryFn,
staleTime,
enabled,
refetchOnRestart,
refetchOnReconnect,
gcTime,
retry,
retryOnMount,
retryDelay,
},
queryClient,
)
Parameter1 (Options)
queryKey: List<Object>- Required
- The query key to use for this query.
- The query key will be hashed into a stable hash. See Query Keys for more information.
- The query will automatically update when this key changes (as long as
enabledis not set tofalse).
queryFn: Future<T> Function()- Required
- The function that the query will use to request data. Must return a
Future<T>.
enabled: bool?- Optional
- Defaults to
queryClient.defaultOptions.queries.enabledwhen not specified. - Set this to
falseto disable this query from automatically running (useful for dependent queries).
staleTime: double?- Optional
- The time in milliseconds after which data is considered stale. If
null, the client's defaultstaleTimeis used.
refetchOnRestart: bool?- Optional
- When
true, refetches on app restart.
refetchOnReconnect: bool?- Optional
- When
true, refetches on reconnect.
gcTime: int?- Optional
- Garbage-collection time in milliseconds for this query's cache entry. When all observers are removed, the query will be removed from the cache after
gcTimems. A value<= 0disables GC. If unspecified, the client default is used.
retry: dynamic- Optional
- Controls retry behavior; accepts
false,true, anint, or a function(failureCount, error) => bool.
retryOnMount: bool?- Optional
- If
false, a query that currently has an error will not attempt to retry when mounted.
retryDelay: dynamic- Optional
- Milliseconds between retries, or a function
(attempt, error) => intreturning the delay in ms.
Parameter2 (QueryClient)
queryClient: QueryClient?- Optional. Use this to provide a custom
QueryClient; otherwise the one from the nearest context will be used.
- Optional. Use this to provide a custom
Returns
key: String- The internal cache key (string) for this query.
status: QueryStatus- Will be:
pendingif there's no cached data and no query attempt has finished yet.errorif the query attempt resulted in an error; theerrorproperty contains the error.successif the query has received a response with no errors and is ready to display its data.
- Will be:
isPending: bool- A derived boolean from
status.
- A derived boolean from
isSuccess: bool- A derived boolean from
status.
- A derived boolean from
isError: bool- A derived boolean from
status.
- A derived boolean from
data: T?- Defaults to
null. - The last successfully resolved data for the query.
- Defaults to
error: Object?- Defaults to
null. - The error object for the query, if an error was thrown.
- Defaults to
isFetching: booltruewhen the query function is currently executing (includes background refetches).
isStale: booltruewhen the cached data is considered stale.
failureCount: int- The number of consecutive failures for the current fetch cycle.
failureReason: Object?- The last failure reason, if any.
refetch: Future<QueryResult<T>> Function({bool? throwOnError})?- A function to manually refetch the query. Pass
throwOnError: trueto throw on error instead of returning it in the result.
- A function to manually refetch the query. Pass