TypedReducer<State, Action> class

A convenience class for binding Reducers to Actions of a given Type. This allows for type safe Reducers and reduces boilerplate.

Example

In order to see what this utility function does, let's take a look at a regular example of using reducers based on the Type of an action.

// We define out State and Action classes.
class AppState {
  final List<Item> items;

  AppState(this.items);
}

class LoadItemsAction {}
class UpdateItemsAction {}
class AddItemAction{}
class RemoveItemAction {}
class ShuffleItemsAction {}
class ReverseItemsAction {}
class ItemsLoadedAction<Item> {
  final List<Item> items;

  ItemsLoadedAction(this.items);
}

// Then we define our reducer. Since we handle different actions in our
// reducer, we need to determine what kind of action we're working with
// using if statements, and then run some computation in response.
//
// This isn't a big deal if we have relatively few cases to handle, but your
// reducer function can quickly grow large and take on too many
// responsibilities as demonstrated here with pseudo-code.
final appReducer = (AppState state, action) {
  if (action is ItemsLoadedAction) {
    return new AppState(action.items);
  } else if (action is UpdateItemsAction) {
    return ...;
  } else if (action is AddItemAction) {
    return ...;
  } else if (action is RemoveItemAction) {
    return ...;
  } else if (action is ShuffleItemsAction) {
    return ...;
  } else if (action is ReverseItemsAction) {
    return ...;
  } else {
    return state;
  }
};

What would be nice would be to break our big reducer up into smaller reducers. It would also be nice to bind specific Types of Actions to specific reducers so we can ensure type safety for our reducers while avoiding large trees of if statements.

// First, we'll break out all of our individual State Changes into
// individual reducers. These can be easily tested or composed!
final loadItemsReducer = (AppState state, LoadTodosAction action) =>
  return new AppState(action.items);

final updateItemsReducer = (AppState state, UpdateItemsAction action) {
  return ...;
}

final addItemReducer = (AppState state, AddItemAction action) {
  return ...;
}

final removeItemReducer = (AppState state, RemoveItemAction action) {
  return ...;
}

final shuffleItemsReducer = (AppState state, ShuffleItemAction action) {
  return ...;
}

final reverseItemsReducer = (AppState state, ReverseItemAction action) {
  return ...;
}

// We will then wire up specific types of actions to our reducer functions
// above. This will return a new Reducer<AppState> which puts everything
// together!.
final Reducer<AppState> appReducer = combineReducers([
  new TypedReducer<AppState, LoadTodosAction>(loadItemsReducer),
  new TypedReducer<AppState, UpdateItemsAction>(updateItemsReducer),
  new TypedReducer<AppState, AddItemAction>(addItemReducer),
  new TypedReducer<AppState, RemoveItemAction>(removeItemReducer),
  new TypedReducer<AppState, ShuffleItemAction>(shuffleItemsReducer),
  new TypedReducer<AppState, ReverseItemAction>(reverseItemsReducer),
]);
Implemented types

Constructors

TypedReducer(State reducer(State state, Action action))
Creates a reducer that will only be executed if the dispatched action matches the Action type.

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
reducer → State Function(State state, Action action)
A Reducer function that only accepts an action of a specific type
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

call(State state, dynamic action) → State
The Reducer function that converts the current state and action into a new state
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

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