createStructuredSelector
Selector<
Structured selector that computes multiple derived values at once.
This is useful when you need to compute multiple derived values from the same base selectors.
Example:
final todoStats = createStructuredSelector<AppState, TodoStats>(
(state) => (
total: state.todos.length,
completed: state.todos.where((t) => t.completed).length,
active: state.todos.where((t) => !t.completed).length,
),
);
Implementation
Selector<S, R> createStructuredSelector<S, R>(R Function(S state) compute) {
R? lastResult;
S? lastState;
var hasCache = false;
return (S state) {
if (hasCache && identical(state, lastState)) {
return lastResult as R;
}
lastState = state;
lastResult = compute(state);
hasCache = true;
return lastResult as R;
};
}