StateMachine class

A simple, typed finite state machine.

Creating a state machine takes 3 steps:

  1. Instantiate a StateMachine.
  2. Create the set of states.
  3. Create the set of valid state transitions.

To demonstrate:

// 1.
StateMachine machine = new StateMachine('switch');

// 2.
State isOn = machine.newState('on');
State isOff = machine.newState('off');

// 3.
StateTransition turnOn = machine.newStateTransition('turnOn', [isOff], isOn);
StateTransition turnOff = machine.newStateTransition('turnOff', [isOn], isOff);

Once the state machine is setup as desired, it must be started at a specific state. Once started, no additional states or transitions can be added.

machine.start(isOff);

At any point, you can retrieve the current state from the machine:

State current = machine.current;

Once initialized, the state machine is driven by the states and the state transitions. See State and StateTransition for more information.

Constructors

StateMachine(String name)

Properties

current State
State that the machine is currently in.
no setter
didDispose Future<Null>
A Future that will complete when this object has been disposed.
no setterinherited
disposableTypeName String
A type name, similar to runtimeType but intended to work with minified code.
no setter
disposalTreeSize int
The total size of the disposal tree rooted at the current Disposable instance.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isDisposed bool
Whether this object has been disposed.
no setterinherited
isLeakFlagSet bool
Whether the leak flag for this object has been set.
no setterinherited
isOrWillBeDisposed bool
Whether the disposal of this object has been requested, is in progress, or is complete.
no setterinherited
name String
Name of the state machine. Used for debugging.
getter/setter pair
onStateChange Stream<StateChange>
Stream of state change events. This allows States and StateTransitions to listen for state changes and notify listeners as necessary.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

awaitBeforeDispose<T>(Future<T> future) Future<T>
Add future to a list of futures that will be awaited before the object is disposed.
inherited
dispose() Future<Null>
Dispose of the object, cleaning up to prevent memory leaks.
inherited
flagLeak([String? description]) → void
Flag the object as having been disposed in a way that allows easier profiling.
inherited
getManagedDelayedFuture<T>(Duration duration, T callback()) Future<T>
Creates a Future that will complete, with the value returned by callback, after the given amount of time has elapsed.
inherited
getManagedDisposer(Disposer disposer) → ManagedDisposer
Automatically handle arbitrary disposals using a callback.
inherited
getManagedPeriodicTimer(Duration duration, void callback(Timer timer)) Timer
Creates a periodic Timer that will be cancelled if active upon disposal.
inherited
getManagedTimer(Duration duration, void callback()) Timer
Creates a Timer instance that will be cancelled if active upon disposal.
inherited
listenToStream<T>(Stream<T> stream, void onData(T event), {Function? onError, void onDone()?, bool? cancelOnError}) StreamSubscription<T>
Returns a StreamSubscription which handles events from the stream using the provided onData, onError and onDone handlers.
inherited
manageAndReturnTypedDisposable<T extends Disposable>(T disposable) → T
Automatically dispose another object when this object is disposed.
inherited
manageCompleter<T>(Completer<T> completer) Completer<T>
Ensure that a completer is completed when the object is disposed.
inherited
manageDisposable(Disposable disposable) → void
inherited
manageStreamController(StreamController controller) → void
Automatically cancel a stream controller when this object is disposed.
inherited
newState(String name) State
Create a new State for this StateMachine.
newStateTransition(String name, List<State> from, State to) StateTransition
Create a new StateTransition for this StateMachine.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose() Future<Null>
Callback to allow arbitrary cleanup on dispose.
inherited
onWillDispose() Future<Null>
Callback to allow arbitrary cleanup as soon as disposal is requested (i.e. dispose is called) but prior to disposal actually starting.
inherited
start(State startingState) → void
Start the state machine at the given starting state.
toString() String
A string representation of this object.
override

Operators

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