dataState<D> method
void
dataState<D>(
- StateKey stateKey,
- InitialData<
D> initialData, - void build(
- DataStateBuilder<
D> builder
- DataStateBuilder<
- StateKey? parent,
- InitialChild? initialChild,
- StateDataCodec? codec,
Adds to the state tree a description of a data state, identified by stateKey and carrying a
value of type D.
The behavior of the data state is configured by calling methods on the _DataStateBuilder
that is provided to the build callback.
The initial value of the state data is provided by initialData, and will be evaluated when
the state is first entered.
enum Messages { increment, decrement }
var countingState = StateKey('counting');
var builder = new StateTreeBuilder(initialState: countingState);
// Describe a state carrying an integer, with an initial value of 1.
builder.dataState<int>(
countingState,
InitialData.value(1),
(b) {
// Define the behavior of the state
b.onMessageValue<Messages>(Messages.increment, (b) {
b.stay(action: b.act.updateData((ctx, msg, counter) => counter + 1));
});
b.onMessageValue<Messages>(Messages.decrement, (b) {
b.stay(action: b.act.updateData((ctx, msg, counter) => counter - 1));
});
});
The state can be declared as a child state, by providing a parent value referencing the
parent state. If the state is itself a parent state (that is, other states refer to it as a
parent), then initialChild must be provided, indicating which child state should be entered
when this state is entered.
Implementation
void dataState<D>(
StateKey stateKey,
InitialData<D> initialData,
void Function(DataStateBuilder<D> builder) build, {
StateKey? parent,
InitialChild? initialChild,
StateDataCodec? codec,
}) {
if (_stateBuilders.containsKey(stateKey)) {
throw StateError('State $stateKey has already been configured.');
}
var builder = _DataStateBuilder<D>._(stateKey, initialData, codec, parent, initialChild);
build(builder);
_addState(builder);
}