StateTreeBuilder class

Provides methods to describe a state tree.

States are defined and added to the state tree are described by calling methods such state, dataState, and finalState. A state tree or state machine can be nested within another state tree with the machineState method.

enum Messages { toggle }
var offState = StateKey('off');
var onState = StateKey('on');

StateTreeBuilder switchBuilder() {
  // A simple switch with on and off states
  var treeBuilder = StateTreeBuilder(initialState: offState)
  treeBuilder.state(offState, (b) {
    b.onMessageValue(Messages.toggle, (b) => b.goTo(onState));
  })
  treeBuilder.state(onState, (b) {
    b.onMessageValue(Messages.toggle, (b) => b.goTo(offState));
  });
  return treeBuilder;
}

Once a StateTreeBuilder has been initialized with the desired states, it can be used to create a state machine. The state machine will create and manage its own instance of the state tree defined by the builder.

 var stateTreeBuilder = switchBuilder();
 var stateMachine = TreeStateMachine(builder);

Note that a single state tree builder instance can be used to create multiple state machine instances.

A textual description of the state tree can be produced by calling format method, passing a StateTreeFormatter (for example a DotFormatter) representing the desired output format.

Constructors

StateTreeBuilder({required StateKey initialState, String? label, String? logName})
Creates a StateTreeBuilder that will build a state tree that starts in the state identified by initialState.
factory
StateTreeBuilder.withRoot(StateKey rootState, void build(StateBuilder builder), InitialChild initialChild, {String? label, String? logName})
Creates a StateTreeBuilder with a predefined root state.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
label String?
An optional descriptive label for this state tree, for diagnostic purposes.
final
logName String?
An optional name for this state tree that will be used as the suffix of the logger name used when logging messages.
final
rootKey StateKey
The key indentifying the root state of the state tree.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

call([TreeBuildContext? context]) → TreeNode
Creates the root node of the state tree.
dataState<D>(StateKey stateKey, InitialData<D> initialData, void build(StateBuilder<D> builder), {StateKey? parent, InitialChild? initialChild, StateDataCodec? codec}) → void
Adds to the state tree a description of a data state, identified by stateKey and carrying a value of type D.
finalDataState<D>(StateKey stateKey, InitialData<D> initialData, void build(EnterStateBuilder<D> builder), {StateKey? parent, StateDataCodec? codec}) → void
Adds to the state tree a description of a final data state, identified by stateKey and carrying a value of type D. The behavior of the state is configured by the build callback.
finalState(StateKey stateKey, void build(EnterStateBuilder<void> builder), {StateKey? parent}) → void
Adds to the state tree a description of a final state, identified by stateKey. The behavior of the state is configured by the build callback.
format(StringSink sink, StateTreeFormatter formatter) → void
Writes a textual description of the state stree to the sink. The specific output format is controlled by the type of the formatter.
machineState(StateKey stateKey, InitialMachine initialMachine, void build(MachineStateBuilder), {bool isDone(Transition transition)?, StateKey? parent, String? label}) → void
Adds to the state tree a description of a machine state, identifed by stateKey, which will run a nested state machine.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
state(StateKey stateKey, void build(StateBuilder<void> builder), {StateKey? parent, InitialChild? initialChild}) → void
Adds to the state tree a description of a state, identified by stateKey.
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

withDataRoot<D>(StateKey rootState, InitialData<D> initialData, void build(StateBuilder<D> builder), InitialChild initialChild, {StateDataCodec? codec, String? label, String? logName}) StateTreeBuilder
Creates a StateTreeBuilder with a root state carrying a value of type D.

Constants

defaultRootKey → const StateKey
The key identifying the root state that is implicitly added to a state tree, if the new StateTreeBuilder constructor is used.