StateBase<T> class

Every state class must derived from StateBase<T> class. And it is mandatory to pass the state name and initialState.

Example:

class CounterModel {
 int count;
 bool isLoading;

 CounterModel({this.count, this.isLoading});

 copyWith({int count, bool isLoading}) {
   return CounterModel(
       count: count ?? this.count, isLoading: isLoading ?? this.isLoading);
 }

 CounterModel.init() : this(count: 10, isLoading: false);
}

class CounterState extends StateBase<CounterModel> {
 CounterState() : super(name: 'counter', initialState: CounterModel.init());

 Stream<CounterModel> mapActionToState(
     CounterModel state, Action action, Store store) async* {
   switch (action.type) {
     case ActionTypes.Inc:
       state.count++;
       yield state.copyWith(isLoading: false);
       break;
     case ActionTypes.Dec:
       state.count--;
       yield state.copyWith(isLoading: false);
       break;
     case ActionTypes.AsyncInc:
       yield state.copyWith(isLoading: true);
       yield await getCount(state.count);
       break;
     default:
       yield getState(store);
   }
 }

 Future<CounterModel> getCount(int count) {
   return Future.delayed(Duration(milliseconds: 500),
       () => CounterModel(count: count + 1, isLoading: false));
 }
}

Constructors

StateBase({String name, T initialState})

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
initialState → T
final
name String
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

getState(Store store) → T
mapActionToState(T state, Action action, Store store) Stream<T>
This function should be invoked whenever action dispatchd to the store. [...]
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