CombineMemo<T> class
final
Combining results
Remembers the last combination, so a combiner runs only when a source's data really changed and an equal result keeps its instance.
Optional. Without one, combine runs the combiner every time it is called
— every build, in a widget — which is fine for a constructor call and
wasteful for a join over two long lists. Keep one per call site, next to
whatever owns the reads (a State field, say), and pass it as memo:.
The combiner is skipped when every source's data is the identical
instance it was last time — which structural sharing makes the normal case
for a refetch that changed nothing. When it does run, its result is
structurally shared with the previous one, as TanStack Query shares the
result of combine.
The combiner must be a function of the sources and of keys, and of
nothing else. A memo cannot see what a closure captures: a combiner that
filters by a search text it closes over keeps returning the list for the
old text until a source changes. Either do that work on the combined data,
after combine, or name what the combiner reads — keys: [search] —
which is compared with == and re-runs the combiner when it differs.
class _TaskListState extends State<TaskList> {
final _visible = CombineMemo<List<Task>>();
String search = '';
CombinedResult<List<Task>> visible(
QueryResult<List<Task>> tasks,
QueryResult<Set<int>> hidden,
) =>
(tasks, hidden).combine(
(tasks, hidden) => [
for (final task in tasks)
if (!hidden.contains(task.id) && task.title.contains(search))
task,
],
memo: _visible,
keys: [search],
);
}
Constructors
- CombineMemo()
-
An empty memo: the first
combinethrough it runs the combiner.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited