QueryKey class final Queries

An immutable, structurally compared cache key: what identifies a query in the cache.

Two options objects with equal keys address the same cache entry, so a key must describe everything the query function's result depends on — a list key and a detail key per id, a filter object as part of the key.

QueryKey(['tasks']);
QueryKey(['tasks', id]);
QueryKey(['tasks', {'status': 'active', 'project': 'website'}]);

final tasks = QueryKey(['tasks']);
final task = tasks.append([id]);          // QueryKey(['tasks', id])
task.matches(tasks);                      // true: a prefix matches
client.invalidateQueries(filters: QueryFilters(queryKey: tasks));

Filters such as QueryFilters.queryKey match by prefix (matches), so a hierarchical key — the most general part first — lets one call invalidate a whole family of queries.

Unlike TanStack Query, which hashes keys to a JSON string because JavaScript has no structural equality, a key here is a value type with == and hashCode, and the cache is keyed by it directly.

Parts are deep-copied into unmodifiable collections at construction, so a list or map you keep a reference to cannot later corrupt the cache.

Every part must have value equality: primitives, String, DateTime, Duration, enums, collections of those, and user types that implement ==/hashCode (freezed, Equatable, or written by hand). A debug-only assertion rejects parts that use identity equality, because such a key can never match a second, equal key. User objects are retained, not cloned: their equality and hash values must remain immutable for the key's lifetime. Collection graphs must be acyclic; cyclic input is unsupported. Map keys must also have stable value equality.

Parts compare with ==, and 1 == 1.0 holds in Dart on every platform, so QueryKey([1]) and QueryKey([1.0]) are the same key — not a web quirk.

Two DateTime parts compare by instant: a UTC and a local DateTime of the same moment are one key, as they are in TanStack Query, whose JSON hash writes both as the same ISO string (Dart's own DateTime.== also compares the time zone flag). The parts keep what was passed; the entry keeps the key it was created with. This holds only for parts — a DateTime used as a map key inside a part is looked up with its own ==.

What the debug assertion cannot see. It looks at hashCode, so it passes two kinds of part that never match a second, equal-looking key:

  • A record is a leaf, compared with the record's own ==, and that compares each field with its == — a List, Set or Map field by identity. QueryKey([(id, [1, 2])]) is therefore never equal to another key built the same way: every lookup misses and every build creates a new entry. Dart cannot walk a record's fields generically, so this is not checked. Keep collections out of records in keys — put them as parts of their own, or in a map part: QueryKey([id, {'channels': [1, 2]}]).
  • A class that overrides hashCode but not ==.

A map part's keys must have value equality too, and are held to the same test as a part, with one addition: a collection as a map key is refused, because a map part is compared by looking its keys up, and a collection key hashes by identity.

Annotations
  • @immutable

Constructors

QueryKey(List<Object?> parts)
A key of parts, each list, set and map in them deep-copied into an unmodifiable one. In debug builds, asserts that every part has value equality.

Properties

debugString → String
A canonical rendering with map keys sorted, for logs and debugging (the same shape as TanStack Query's hashKey). Not the key's identity: equality is structural, see the class doc.
no setter
hashCode → int
The hash code for this object.
latefinal
parts → List<Object?>
The key's parts, with nested collections copied into unmodifiable ones. User objects remain responsible for their own immutability.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

append(List<Object?> more) → QueryKey
A key with more appended — the idiom for deriving a detail key from a list key.
matches(QueryKey filter, {bool exact = false}) → bool
Whether filter matches this key.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

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