dataState<D> method

void dataState<D>(
  1. StateKey stateKey,
  2. InitialData<D> initialData,
  3. void build(
    1. StateBuilder<D> builder
    ), {
  4. StateKey? parent,
  5. InitialChild? initialChild,
  6. 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 StateBuilderthat is provided to the build callback.

The initial value of the state data is provided by initialData, and will be evaluated each time the state is entered.

var countingState = StateKey('counting');
var builder = StateTreeBuilder(initialState: countingState);

// Describe a state carrying an integer, with an initial value of 1.
builder.dataState<int>(countingState, InitialData(() => 1), (b) {
  // Define the behavior of the state
  b.onMessageValue<Messages>(Messages.increment, (b) {
    // The updateOwnData callback is called with a context that provides the current message
    // being processed, the and data value for the state.
    b.stay(action: b.act.updateOwnData((ctx) => ctx.data + 1));
  });
  b.onMessageValue<Messages>(Messages.decrement, (b) {
    b.stay(action: b.act.updateOwnData((ctx) => ctx.data - 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(StateBuilder<D> builder) build, {
  StateKey? parent,
  InitialChild? initialChild,
  StateDataCodec? codec,
}) {
  var builder = StateBuilder<D>._(
    stateKey,
    initialData,
    _log,
    parent,
    initialChild,
    isFinal: false,
    codec: codec,
  );
  build(builder);
  _addState(builder);
}