combineReducersMap

Reducer<Map<String, Object?>> combineReducersMap(

  1. Map<String, Reducer<Object?>> reducers
)

A simpler way to combine reducers using a Map-based state.

This is closer to Redux's original combineReducers API but with less type safety. Prefer combineReducers for fully typed state.

Example:

final reducer = combineReducersMap({
  'counter': counterReducer,
  'todos': todosReducer,
});

Implementation

Reducer<Map<String, Object?>> combineReducersMap(
  Map<String, Reducer<Object?>> reducers,
) => (state, action) {
  var hasChanged = false;
  final nextState = <String, Object?>{};

for (final entry in reducers.entries) { final key = entry.key; final reducer = entry.value; final previousStateForKey = state[key]; final nextStateForKey = reducer(previousStateForKey, action);

nextState[key] = nextStateForKey; hasChanged = hasChanged || !identical(previousStateForKey, nextStateForKey); }

return hasChanged ? nextState : state; };