EventStateMachine<StateType, EventType> class abstract

{@template state_machine} A Bloc that provides facilities methods to create state machines

The state machine uses Bloc's on<Event> method under the hood with a custom event dispatcher that will in turn call your methods and callbacks.

State machine's states should be defined with the StateMachine's define<State> methods inside the constructor. You should never try to transit to a state that hasn't been explicitly defined. If the state machine detects a transition to an undefined state, it will throw an error.

Each state has its own set of event handlers and side effects callbacks:

  • Event handlers react to an incoming event and can emit the next machine's state. We call this a transition.
  • Side effects are callback functions called depending on state lifecycle. You have access to three different side effects: onEnter, onExit, and onChange.

When an event is received, the state machine will first search for the actual state definition. Each current state's event handler that matches the received event type will be evaluated. If multiple events handlers match the event type, they will be evaluated in their definition order. As soon as an event handler returns a non-null state (we call this entering a transition), the state machine stops evaluating events handlers and transit to the new state immediately.

class MyStateMachine extends StateMachine<Event, State> {
MyStateMachine() : super(InitialState()) {
   define<InitialState>(($) => $
     ..onEnter((InitialState state) { /** ... **/ })
     ..onChange((InitialState state, InitialState nextState) { /** ... **/ })
     ..onExit((InitialState state) { /** ... **/ })
     ..on<SomeEvent>((SomeEvent event, InitialState state) => OtherState())
   );
   define<OtherState>();
  }
}

See also:

  • Bloc class for more information about general blocs behavior {@endtemplate state_machine}
Inheritance
Implementers

Constructors

EventStateMachine(StateType initial)

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state ↔ StateType
getter/setter pairoverride
states Set<StateDefinitionBuilder>
no setter

Methods

add(EventType event) → void
define<DefinedState extends StateType>([StateDefinitionBuilderCallback<StateType, EventType, DefinedState>? delegate]) StateDefinitionBuilder<StateType, EventType, DefinedState>
Register DefinedState as one of the allowed machine's states.
ifState<SpecificState extends StateType>(dynamic callback(SpecificState)) → void
inherited
isInState<SpecificState extends StateType>() bool
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onTransition(covariant Transition<dynamic, StateType> transition) → void
Called whenever a change occurs with the given change. A change occurs when a new state is emitted. onChange is called before the state of the cubit is updated. onChange is a great spot to add logging/analytics for a specific cubit.
inherited
toString() String
A string representation of this object.
inherited

Operators

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