我只使用了 Flutter_redux 几天,我想知道这之间有什么区别:
class BtnCustom extends StatelessWidget {
@override
Widget build(BuildContext context) {
final store = StoreProvider.of<AppState>(context);
return FlatButton(
onPressed: store.dispatch(MyCustomAction),
child: Text(store.state.MyCustomTxt),
);
}
}
和
class BtnCustom extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, _ViewModel>(
converter: (store) => _ViewModel(
txt: store.state.MyCustomTxt,
onPressed: store.dispatch(MyCustomAction)),
builder: (BuildContext context, _ViewModel vm) {
return FlatButton(
onPressed: vm.onPressed,
child: Text(vm.txt),
);
},
);
}
}
class _ViewModel {
final String txt;
final void Function() onPressed;
_ViewModel({this.txt, this.onPressed});
}
?
第一个看起来很方便使用。我应该注意使用其中一种方法有什么优点或缺点吗?
根据文档,StoreConnector 将在其中重建小部件,因此:
- 当您不需要重建小部件时,可以不使用 StoreConnector 吗?
- 可以在 StoreConnector 中有多个小部件吗?