DeclarativeStateTreeBuilder 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');

DeclarativeStateTreeBuilder switchBuilder() {
  // A simple switch with on and off states
  var treeBuilder = DeclarativeStateTreeBuilder(initialChild: 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 DeclarativeStateTreeBuilder 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 declBuilder = switchBuilder();
 var stateMachine = TreeStateMachine(StateTreeBuilder(declBuilder));

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

DeclarativeStateTreeBuilder({required StateKey initialChild, String? label, String? logName})
Creates a DeclarativeStateTreeBuilder that will build a state tree that starts in the state identified by initialChild.
factory
DeclarativeStateTreeBuilder.withRoot(StateKey rootState, InitialChild initialChild, void build(StateBuilder<void> builder), {String? label, String? logName, void extensions(StateExtensionBuilder)?})
Creates a DeclarativeStateTreeBuilder 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

createRootNodeInfo() → RootNodeInfo
Creates a RootNodeInfo that can be used by StateTreeBuilder to build a state tree.
dataState<D>(DataStateKey<D> stateKey, InitialData<D> initialData, void build(StateBuilder<D> builder), {StateKey? parent, InitialChild? initialChild, StateDataCodec? codec}) StateExtensionBuilder
Adds to the state tree a description of a data state, identified by stateKey and carrying a value of type D.
extendState(StateKey stateKey) StateExtensionBuilder
Returns a StateExtensionBuilder that can be used to extend the state identified by stateKey with additional metadata and filters.
extendStates(void extend(StateKey, StateExtensionBuilder)) DeclarativeStateTreeBuilder
Calls the extend function for each state that has been defined, allowing the states to be extended with additional metadata and filters.
finalDataState<D>(DataStateKey<D> 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(DataStateKey<MachineTreeStateData> 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}) StateExtensionBuilder
Adds to the state tree a description of a state, identified by stateKey.
toString() String
A string representation of this object.
inherited
toTreeBuilder() → StateTreeBuilder

Operators

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

Static Methods

withDataRoot<D>(DataStateKey<D> rootState, InitialData<D> initialData, void build(StateBuilder<D> builder), InitialChild initialChild, {StateDataCodec<D>? codec, String? label, String? logName}) DeclarativeStateTreeBuilder
Creates a DeclarativeStateTreeBuilder 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 StateTreeBuilder.new constructor is used.