QueriesObserver<TQueryData, TData> class
Observers
Watches a list of queries at once and reports their results as one list, in the order the queries were given.
Use it when the number of queries is only known at run time — one query
per id in a list, say. Each query is fetched, cached and settles on its
own, exactly as with a QueryObserver per query (which is what this
holds, one per entry); the listener receives the whole list of
QueryResults whenever any of them changes. All queries share one data
type TQueryData and one reported type TData; for a fixed handful of
queries of different types, observe them separately and combine the
results with combine on a record of results.
The lifecycle mirrors QueryObserver's:
- Construct with a client and a list of options. The queries are built or joined, but nothing is fetched yet.
- subscribe a listener. The first listener subscribes every member, which fetches each one that is due.
- setQueries replaces the list — members whose key is still there are kept (and handed their new options), new keys get a new member, and members whose key is gone are dropped.
- Stop: removing the last listener unsubscribes every member; destroy also releases the members themselves.
final observer = QueriesObserver(client, [
for (final id in taskIds)
QueryObserverOptions<Task>(
queryKey: QueryKey(['tasks', id]),
queryFn: (_) => api.task(id),
),
]);
final unsubscribe = observer.subscribe((results) {
final loaded = results.whereType<QuerySuccess<Task>>().length;
print('$loaded of ${results.length} tasks loaded');
});
// When the ids change: observer.setQueries([...]).
// When done:
unsubscribe();
The same key may appear more than once; each occurrence gets its own
member, so two entries can apply different selects or options to the
same cached data.
Constructors
-
QueriesObserver(QueryClient _client, List<
QueryObserverOptionsBase< queries)TQueryData, TData> > -
Creates the members for
queries— building or joining each query — without fetching until the first subscribe.
Properties
-
currentResult
→ List<
QueryResult< TData> > -
The latest result for each query, in the order of the list last
given to the constructor or setQueries. Read-only; a new list
replaces it whenever any member's result changes.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether anyone is subscribed. While this is false, the members are not
subscribed to their queries and nothing is fetched on their behalf.
no setter
-
observers
→ List<
QueryObserver< TQueryData, TData> > -
The underlying observers, in input order. The returned list is read-only;
their lifetime belongs to this collection.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
destroy(
) → void -
Stops observing for good: removes every listener, unsubscribes every
member and destroys it, so each query starts its
gcTimeclock once nothing else observes it. -
getOptimisticResult(
) → List< QueryResult< TData> > - Each member's QueryObserver.getOptimisticResult for its current options: the results as they will look once subscribed, a fetch about to start included — the right read for a UI's first build, before the subscription exists. Fetches nothing.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setQueries(
List< QueryObserverOptionsBase< queries) → voidTQueryData, TData> > - Replaces the list of queries.
-
subscribe(
void listener(List< QueryResult< )) → void Function()TData> > -
Registers
listenerand returns the function that removes it again. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited