createSelector1
Selector<
- T1
Creates a memoized selector that caches its result.
The selector will only recompute when the input changes. This is useful for expensive computations derived from state.
Example:
final getTodos = (AppState state) => state.todos;
final getFilter = (AppState state) => state.filter;
final getVisibleTodos = createSelector1(
getTodos,
(todos) => todos.where((t) => !t.completed).toList(),
);
Implementation
Selector<S, R> createSelector1<S, T1, R>(
Selector<S, T1> selector1,
R Function(T1) combiner,
) {
T1? lastInput;
R? lastResult;
var hasCache = false;
return (S state) {
final input = selector1(state);
if (hasCache && identical(input, lastInput)) {
return lastResult as R;
}
lastInput = input;
lastResult = combiner(input);
hasCache = true;
return lastResult as R;
};
}