QueryClient class
Base Client for managing Query, InfiniteQuery and Mutation objects and all related configuration
QueryClient is the basic imperative API to handle and manage Queries and Mutations and used internally by the Declarative wrapper widgets and hooks
Usually, it can be helpful to use when only data modification is needed without any UI changes e.g. after completing an action
It can be accessed anywhere in the widget tree using QueryClient.of or QueryClient.maybeOf
final queryClient = QueryClient.of(context);
await queryClient.refreshQuery('todos');
If you don't have access to BuildContext e.g. in a Provider or BLoC
you can initialize your own QueryClient globally and pass it QueryClientProvider
and use it anywhere in the widget tree
final queryClient = QueryClient();
QueryClientProvider(
client: queryClient,
child: (....)
)
// Somewhere else in the project
import 'package:example/config/query_client.dart';
class TodoListNotifier extends ChangeNotifier {
void addTodo(Todo todo) async {
final res = post(api, todo);
await queryClient.refreshQuery('todos');
}
}
- The above can be also implemented using just by Mutation
- Annotations
Constructors
- QueryClient({QueryCache? cache, Duration cacheDuration = DefaultConstants.cacheDuration, int? maxRetries, Duration? retryDelay, Duration? staleDuration, Duration? refreshInterval, bool? refreshOnMount, bool? refreshOnQueryFnChange})
Properties
- cache → QueryCache
-
final
- cacheDuration → Duration
-
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- refreshConfig → RefreshConfig
-
final
- retryConfig → RetryConfig
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createInfiniteQuery<
DataType, ErrorType, PageType> (String key, InfiniteQueryFn< DataType, PageType> queryFn, {required InfiniteQueryNextPage<DataType, PageType> nextPage, required PageType initialPage, RetryConfig? retryConfig, RefreshConfig? refreshConfig, JsonConfig<DataType> ? jsonConfig}) → InfiniteQuery<DataType, ErrorType, PageType> - Creates + stores an InfiniteQuery
-
createMutation<
DataType, ErrorType, VariablesType> (String key, MutationFn< DataType, VariablesType> mutationFn, {RetryConfig? retryConfig}) → Mutation<DataType, ErrorType, VariablesType> - Creates a new Mutation
-
createQuery<
DataType, ErrorType> (String key, QueryFn< DataType> queryFn, {DataType? initial, RetryConfig? retryConfig, RefreshConfig? refreshConfig, JsonConfig<DataType> ? jsonConfig}) → Query<DataType, ErrorType> - Imperatively creates a Query
-
fetchInfiniteQuery<
DataType, ErrorType, PageType> (String key, InfiniteQueryFn< DataType, PageType> queryFn, {required InfiniteQueryNextPage<DataType, PageType> nextPage, required PageType initialPage, RetryConfig? retryConfig, RefreshConfig? refreshConfig, JsonConfig<DataType> ? jsonConfig}) → Future<DataType?> - Creates + stores an InfiniteQuery and fetches the first page immediately and returns the result
-
fetchInfiniteQueryJob<
DataType, ErrorType, PageType, ArgsType> ({required InfiniteQueryJob< DataType, ErrorType, PageType, ArgsType> job, required ArgsType args}) → Future<DataType?> -
fetchQuery<
DataType, ErrorType> (String key, QueryFn< DataType> queryFn, {DataType? initial, RetryConfig? retryConfig, RefreshConfig? refreshConfig, JsonConfig<DataType> ? jsonConfig}) → Future<DataType?> -
Creates + stores a Query and runs the
queryFnimmediately and returns the result -
fetchQueryJob<
DataType, ErrorType, ArgsType> ({required QueryJob< DataType, ErrorType, ArgsType> job, required ArgsType args}) → Future<DataType?> -
getInfiniteQueries(
List< String> keys) → List<InfiniteQuery> -
getInfiniteQueriesWithPrefix(
String prefix) → List< InfiniteQuery> -
Finds all the InfiniteQuery that starts with the given
prefix -
getInfiniteQuery<
DataType, ErrorType, PageType> (String key, {bool exact = true}) → InfiniteQuery< DataType, ErrorType, PageType> ? -
Finds the InfiniteQuery with the given
key -
getMutation<
DataType, ErrorType, VariablesType> (String key, {bool exact = true}) → Mutation< DataType, ErrorType, VariablesType> ? - Finds the Mutation
-
getQueries(
List< String> keys) → List<Query> -
getQueriesWithPrefix(
String prefix) → List< Query> -
Finds all the Query that starts with the given
prefix -
getQuery<
DataType, ErrorType> (String key, {bool exact = true}) → Query< DataType, ErrorType> ? -
Finds the Query with the given
keyand returns it -
mutateMutation<
DataType, ErrorType, VariablesType> (String key, VariablesType variables, {MutationFn< DataType, VariablesType> ? mutationFn, RetryConfig? retryConfig, List<String> refreshQueries = const [], List<String> refreshInfiniteQueries = const []}) → Future<DataType?> -
Finds the Mutation with the given
keyand runs Mutation.mutate -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refreshInfiniteQueries(
List< String> keys) → Future<List> -
refreshInfiniteQueriesAllPages(
List< String> keys) → Future<Map< String, List?> > -
refreshInfiniteQueriesAllPagesWithPrefix(
String prefix) → Future< Map< String, List?> > -
Finds all the InfiniteQuery that starts with the given
prefixand refreshes all pages using InfiniteQuery.refreshAll -
refreshInfiniteQueriesWithPrefix(
String prefix) → Future< List> -
Finds all the InfiniteQuery that starts with the given
prefixand refreshes using InfiniteQuery.refresh -
refreshInfiniteQuery<
DataType, ErrorType, PageType> (String key, {PageType? page, bool exact = true}) → Future< DataType?> -
Finds the InfiniteQuery with the given
keyand refreshes using InfiniteQuery.refresh -
refreshInfiniteQueryAllPages<
DataType, ErrorType, PageType> (String key, {bool exact = true}) → Future< List< DataType> ?> -
Finds the InfiniteQuery with the given
keyand refreshes all pages using InfiniteQuery.refreshAll -
refreshQueries(
List< String> keys) → Future<List> -
refreshQueriesWithPrefix(
String prefix) → Future< List> -
Finds all the Query that starts with the given
prefixand refreshes -
refreshQuery<
DataType, ErrorType> (String key, {bool exact = true}) → Future< DataType?> -
Finds the Query with the given
keyand refreshes using Query.refresh -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- connectivity ↔ ConnectivityAdapter
-
latefinal
- infiniteQueryCachePrefix → String
-
no setter
- queryCachePrefix → String
-
no setter
Static Methods
-
initialize(
{required String cachePrefix, ConnectivityAdapter? connectivity, String? cacheDir}) → Future< void> - Initializes the QueryClient
-
maybeOf(
BuildContext context) → QueryClient? - Gets the QueryClient from the BuildContext if available
-
of(
BuildContext context) → QueryClient - Gets the QueryClient from the BuildContext if available