SubHandler<Model, Msg> class
Provides dispatch and model access within a subscription's setup function.
handler(msg) / handler.dispatch(msg) — send a message.
handler.model — read the current model (always fresh, never stale).
Avoiding stale model captures
The subscriptions(Model model) function is called with the current model
on every model change, but a subscription's setup function is only invoked
once when its ID first appears. Any model value captured via closure
from the outer call will be frozen at that instant and will not update.
Wrong — model is captured and goes stale:
Subs<MyModel, MyMsg> subscriptions(MyModel model) => [
(['my-sub'], (handler) {
return stream.listen((_) => handler(DoThing(model.count))).cancel;
// ^^^^^^^^^^^
// stale after first registration
}),
];
Correct — read handler.model at event time:
Subs<MyModel, MyMsg> subscriptions(MyModel model) => [
(['my-sub'], (handler) {
return stream.listen((_) => handler(DoThing(handler.model.count))).cancel;
}),
];
Preferred (Elm-style) — emit a plain message and let update access
the model, which always receives the freshest state:
Subs<MyModel, MyMsg> subscriptions(MyModel model) => [
(['my-sub'], (handler) {
return stream.listen((_) => handler(EventOccurred())).cancel;
}),
];
// Then in update:
(MyModel, Cmd<MyMsg>) update(MyMsg msg, MyModel model) => switch (msg) {
EventOccurred() => (model.copyWith(count: model.count + 1), Cmd.none()),
// ...
};
Constructors
-
SubHandler(Dispatch<
Msg> _dispatch, Model _getModel()) -
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- model → Model
-
The current model — always reflects the latest state.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
Msg msg) → void -
Dispatch a message. Can also be invoked directly as
handler(msg). -
dispatch(
Msg msg) → void - Dispatch a message.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited