本文介绍了Flutter BLoC-如何将参数传递给事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图学习BLoC,我想到了这个问题.我有一些代码可以在其中生成带有BLoC模式的按钮.但是,我不知道如何使用dispatch(event)
方法更新特定的按钮属性.如何将参数传递给事件ChangeSomeValues
??
Trying to learn BLoCs I came up with this problem. I have some code in which I generate some buttons with BLoC pattern. However, I have no clue how to update specific buttons properties with dispatch(event)
method. How to pass parameters to the event ChangeSomeValues
??
使用BLoC的部分
BlocBuilder(
bloc: myBloc,
builder: (context, state) {
return ListView.builder(
itemCount: state.buttonList.length,
itemBuilder: (context, index) {
return MyButton(
label: buttonList[index].label,
value: buttonList[index].value,
onPressed: myBloc.dispatch(ChangeSomeValues()),
);
}
);
}
),
MyBloc.dart
MyBloc.dart
class MyBloc extends Bloc<MyEvent, MyState> {
@override
Stream<MyState> mapEventToState(MyEvent event) async* {
if (event is ChangeSomeValues) {
... modify specific parameters in list here ...
yield MyState1(modifiedList);
}
}
}
我知道如何使用事件来更改值,但找不到这种通用实现来编辑列表中的特定参数.
I know how to use the events to change values but I couldn't find how to edit specific parameters in list with this kind of a generic implementation.
推荐答案
事件代码:
class ChangeSomeValues extends MyEvent {
final int data;
ChangeSomeValues(this.data);
}
将其发送为:myBloc.dispatch(ChangeSomeValues(15))
集团
class MyBloc extends Bloc<MyEvent, MyState> {
@override
Stream<MyState> mapEventToState(MyEvent event) async* {
if (event is ChangeSomeValues) {
print("here's the data : ${event.data}");
}
}
}
这篇关于Flutter BLoC-如何将参数传递给事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!