UseReducer<T> class

A RtHook that manages state using reducer method.

UseReducer accepts a reducer method and returns the current state paired with a dispatch method. (If you’re familiar with Redux, you already know how this works.)

Contains a value of type T which represents the current state.

When value is different to previous state, UseReducer execute update to notify to listeners that has changed and in turn executes onWillUpdate and onDidUpdate.

Example:

class Store {
  final int count;

  Store({this.count = 0});
}

 Store _reducer(Store state, RtAction<String, int?> action) {
   switch (action.type) {
     case 'increment':
       return Store(count: state.count + (action.payload ?? 1));
     case 'decrement':
       return Store(count: state.count + (action.payload ?? 1));
     default:
       throw UnimplementedError();
   }
 }

class AppController {
  late final state = UseReducer(_reducer, Store(count: 0));

  AppController() {
    print("count: ${state.value.count}"); // count: 0;
    state.dispatch(RtAction(type: 'increment', payload: 2));
    print("count: ${state.value.count}"); // count: 2;
    state.dispatch(RtAction(type: 'decrement'));
    print("count: ${state.value.count}"); // count: 1;
  }
}
Inheritance

Constructors

UseReducer(Reducer<T> reducer, T initialState)

Properties

$ → HookRegister
This variable is used to register Hook and attach the StateBase that are defined here.
final
dependencyInjection → DependencyInjection
no setterinherited
eventHandler → EventHandler
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
instanceBinded Object?
The reference instance to the current state.
no setterinherited
isDisposed bool
Returns true if the state has been disposed.
no setterinherited
logger → Logger
no setterinherited
reducer Reducer<T>
Calculates a new state with state(T) and action(RtAction) given.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stateManagment → StateManagement<StateBase>
no setterinherited
value → T
no setter

Methods

bind(Object instance) → void
Stores a reference to an object instance
inherited
dispatch<A extends RtAction>(A action) → void
Receives a RtAction and sends it to reducer method for resolved
dispose() → void
Called when this object is removed
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
refresh() → void
It's used to notify listeners that the state has been updated. It is typically called after making changes to the state object.
inherited
toString() String
A string representation of this object.
inherited
unbind() → void
Removes the reference to the object instance
inherited
update([covariant Function? callback]) → void
Executes callback, and notifies the listeners about the update.
inherited

Operators

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