我在使用redux和flutter_redux包的Dart代码中使用以下代码。一切都运行良好,但是如你所见,我必须使用以下语法来调用适当的操作。
callback['onIncrement'](34)
它能够工作,但是很丑陋。我该如何改进它以使其更像这样:
callback.onIncrement(34)
这里是代码:
StoreConnector<AppState,Map<String,Function>>(
converter: (store) {
return {
'onIncrement': (value) => store.dispatch(IncrementAction(value)),
'onDecrement': () => store.dispatch(Actions.decrement)
};
},
builder: (context, callback) {
return Column(
children: <Widget>[
FlatButton(
child: Text('Increment Counter', style: TextStyle(color: Colors.white)),
color: Colors.orange,
onPressed: () => callback['onIncrement'](1)
更新:
现在我正在使用枚举,并且效果有所提升:
StoreConnector<AppState,Map<Actions,Function>>(
converter: (store) {
return {
Actions.increment: (value) => store.dispatch(IncrementAction(value)),
Actions.decrement: () => store.dispatch(Actions.decrement)
};
},
builder: (context, callback) {
return Column(
children: <Widget>[
FlatButton(
child: Text('Increment Counter', style: TextStyle(color: Colors.white)),
color: Colors.orange,
onPressed: () => callback[Actions.increment](23)