Lens<GlobalState, LocalState> typedef

Lens<GlobalState, LocalState> = ({LocalState Function(GlobalState state) get, GlobalState Function(GlobalState state, LocalState localState) set})

A bidirectional accessor for reading and writing a LocalState within a GlobalState.

Lenses enable modular state decomposition. They are used with Reducer.pullback to lift a local reducer into a global state domain.

  • get extracts the local state from the global state.
  • set returns a new global state with the local state updated.
Lens<AppState, int> counterLens = (
  get: (app) => app.counter,
  set: (app, counter) => app.copyWith(counter: counter),
);

Implementation

typedef Lens<GlobalState, LocalState> = ({
  LocalState Function(GlobalState state) get,
  GlobalState Function(GlobalState state, LocalState localState) set,
});