updateData<D> method
_MessageAction<M>
updateData<D>(
- D update(
- MessageContext msgCtx,
- M msg,
- D current
- StateKey? forState,
- 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,
);
}