-
Notifications
You must be signed in to change notification settings - Fork 213
Open
Description
While explicit ViewModel
classes are a useful abstraction layer, they can be cumbersome to use when such pure decoupling of the state and UI layer is not a high priority.
Records seem to provide a nice middle ground, allowing for multiple fields to be passed into a StoreConnector
without a dedicated class. An exception does need to be made for functions, though, as anonymous lambdas are never equal.
I've settled on something like the following to use records in this way. props
is used for state and equality, and actions
is used to hold lambdas.
What are everyone's thoughts? Does this seem reasonable? Am I accidentally violating any architectural patterns?
class RecordViewModel<Props extends Record, Actions extends Record> {
final Props props;
final Actions actions;
const RecordViewModel(this.props, this.actions);
@override
bool operator ==(Object other) =>
other is RecordViewModel && props == other.props;
@override
int get hashCode => props.hashCode;
}
StoreConnector(
distinct: true,
converter: (store) => RecordViewModel(
(myData: store.state.myData),
(refresh: () => store.dispatch(const RefreshAction())),
),
builder: (context, vm) {
return MyView(
data: vm.props.myData,
refresh: vm.actions.refresh,
);
},
),
Metadata
Metadata
Assignees
Labels
No labels