updateData<D> method

_MessageAction<M> updateData<D>(
  1. D update(
    1. MessageContext msgCtx,
    2. M msg,
    3. D current
    ), {
  2. StateKey? forState,
  3. String? label,
})

Updates state data of type D while a message is being handled.

When update function is called, it is provided a MessageContext, the message that is being handled, and the current state data value.

enum Messages { increment  }
var countingState = StateKey('counting');
var builder = new StateTreeBuilder(initialState: countingState);

builder.dataState<int>(
  countingState,
  InitialData.value(1),
  (b) {
    b.onMessageValue<Messages>(Messages.increment, (b) {
      // Updates state data as a side effect while the message is handled.
      b.stay(action: b.act.updateData((ctx, msg, counter) => counter + 1));
    });
  });

This action can be labeled when formatting a state tree by providing a label.

Implementation

_MessageAction<M> updateData<D>(
  D Function(MessageContext msgCtx, M msg, D current) update, {
  StateKey? forState,
  String? label,
}) {
  return _MessageAction<M>._(
    _ActionType.updateData,
    (msgCtx, msg) {
      _logger.fine(() => "State '$_forState' is updating data of type $D");
      msgCtx.dataOrThrow<D>(forState).update((d) => update(msgCtx, msg, d));
    },
    null,
    label,
  );
}