Reducer<State, Action, Environment> class final

A pure function that processes an action against the current state and returns the new state along with any Effects to execute.

Reducers are the heart of the Composable Architecture. They are pure, deterministic, and composable. A reducer never performs side effects directly; instead, it returns Effect values that describe the work to be done.

Creating Reducers

// State-only transformation (no effects)
final counterReducer = Reducer<int, CounterAction, EmptyEnvironment>.transform(
  (state, action, env) => switch (action) {
    CounterAction.increment => state + 1,
    CounterAction.decrement => state - 1,
  },
);

// Full reducer with effects
final userReducer = Reducer<UserState, UserAction, UserEnv>(
  reduce: (state, action, env) => switch (action) {
    LoadUser() => (
      state: state.copyWith(isLoading: true),
      effect: Effect.task(() => env.api.fetchUser())
        .map((user) => UserAction.loaded(user)),
    ),
    UserLoaded(:final user) => (
      state: state.copyWith(user: user, isLoading: false),
      effect: Effect.none(),
    ),
  },
);

Composition

Reducers compose via combine, pullback, forEach, and ifLet:

final appReducer = Reducer.combine([
  counterReducer.pullback(stateLens: ..., actionLens: ...),
  userReducer.pullback(stateLens: ..., actionLens: ...),
]);
Available extensions

Constructors

Reducer({required ({Effect<Action> effect, State state}) reduce(State state, Action action, Environment environment)})
Creates a reducer with the given reduce function.
const
Reducer.combine(List<Reducer<State, Action, Environment>> reducers)
Combines multiple reducers into one.
factory
Reducer.emit(Effect<Action> emitter(State state, Action action, Environment env))
Creates a reducer that emits effects without changing state.
factory
Reducer.empty()
Creates a no-op reducer that returns the state unchanged and emits no effects.
factory
Reducer.transform(State transformer(State state, Action action, Environment env))
Creates a reducer that transforms state without producing effects.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
reduce → ({Effect<Action> effect, State state}) Function(State state, Action action, Environment environment)
The reduce function that processes (state, action, environment) and returns (state, effect).
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

debug() Reducer<State, Action, Environment>
Wraps this reducer to print each action, previous state, and new state to the console. Useful during development for tracing state changes.
forEach<GlobalState, GlobalAction, GlobalEnvironment, ID>({required Lens<GlobalState, Iterable<State>> stateLens, required Prism<GlobalAction, Action, ID> actionPrism, required ID toID(State state), Environment toLocalEnvironment(ID, GlobalEnvironment)?}) Reducer<GlobalState, GlobalAction, GlobalEnvironment>

Available on Reducer<State, Action, Environment>, provided by the ForEachIterableReducer extension

Applies this reducer to a specific element in a collection, identified by toID.
forEachIndexed<GlobalState, GlobalAction, GlobalEnvironment>({required Lens<GlobalState, Iterable<State>> stateLens, required Prism<GlobalAction, Action, int> actionPrism, required Environment toLocalEnvironment(int, GlobalEnvironment)}) Reducer<GlobalState, GlobalAction, GlobalEnvironment>

Available on Reducer<State, Action, Environment>, provided by the ForEachIterableReducer extension

Like forEach, but identifies elements by their index in the collection rather than a custom ID function.
forEachMap<GlobalState, GlobalAction, GlobalEnvironment, ID>({required Map<ID, State> toMapState(GlobalState), required GlobalState toGlobalState(GlobalState, Map<ID, State>), required (ID, Action)? toLocalAction(GlobalAction), required GlobalAction toGlobalAction(ID, Action), required Environment toLocalEnvironment(ID, GlobalEnvironment)}) Reducer<GlobalState, GlobalAction, GlobalEnvironment>

Available on Reducer<State, Action, Environment>, provided by the ForEachIterableReducer extension

Like forEach, but operates on a Map<ID, State> instead of an Iterable<State>.
ifLet<ChildState, ChildAction, ChildEnvironment>({required Lens<State, ChildState?> stateLens, required ActionLens<Action, ChildAction> actionLens, ChildEnvironment toLocalEnvironment(Environment)?, required Reducer<ChildState, ChildAction, ChildEnvironment> child}) Reducer<State, Action, Environment>
Composes this reducer with a child reducer that operates on optional state.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
nullable() Reducer<State?, Action, Environment>
Wraps this reducer to handle nullable state.
pullback<GlobalState, GlobalAction, GlobalEnvironment>({required Lens<GlobalState, State> stateLens, required ActionLens<GlobalAction, Action> actionLens, Environment toLocalEnvironment(GlobalEnvironment globalEnv)?}) Reducer<GlobalState, GlobalAction, GlobalEnvironment>
Lifts this local reducer to operate within a larger global domain.
pullbackAction<GlobalAction, GlobalEnvironment>({required ActionLens<GlobalAction, Action> actionLens, Environment toLocalEnvironment(GlobalEnvironment globalEnv)?}) Reducer<State, GlobalAction, GlobalEnvironment>
Lifts this reducer to a global action domain while keeping the same state type. Useful when the state is shared but actions differ.
pullbackState<GlobalState, GlobalEnvironment>({required Lens<GlobalState, State> stateLens, Environment toLocalEnvironment(GlobalEnvironment globalEnv)?}) Reducer<GlobalState, Action, GlobalEnvironment>
Lifts this reducer to a global state domain while keeping the same action type. Useful when the action type is shared across domains.
pullbackWithEnv<GlobalState, GlobalAction, GlobalEnvironment>({required Lens<GlobalState, State> stateLens, required ActionLens<GlobalAction, Action> actionLens, Environment toLocalEnvironment(GlobalEnvironment globalEnv)?}) Reducer<GlobalState, GlobalAction, GlobalEnvironment>
Same as pullback with explicit environment transformation.
toString() String
A string representation of this object.
inherited
when<SubState>(Reducer<SubState, Action, Environment> subReducer) Reducer<State, Action, Environment>
Conditionally applies subReducer when the current state is of type SubState.

Operators

operator ==(Object other) bool
The equality operator.
inherited