【发布时间】:2021-02-22 22:59:43
【问题描述】:
可以这么说,我有一个关于 bloc 正确代码放置的问题。
因此,由于 bloc 代表业务逻辑组件,我已经将业务逻辑(如处理数据方法)移到了类中。所以现在有两种方法,一种用于获取数据,另一种用于保存数据。
接下来,我的组件在AddInitial 中应该显示加载,然后触发块说“嘿,我需要这里的数据”。它是正确的还是反模式?代码如下所示。
BlocBuilder<AddBloc, AddState>(
buildWhen: (previous, current) {
// code is ommited
},
builder: (context, state) {
if (state is ExpensesLoaded || state is CategoryChanged) {
// code is ommited
} else if (state is ExpensesLoading) {
return getProgressSpinner();
} else if (state is AddInitial) {
// is it ok? If it's antipattern please give an advise how to do it correctly
context.bloc<AddBloc>().add(CategoryChange([category]));
return getProgressSpinner();
} else {
print('Bloc Loaded. Nothing to show inside Month Expenses...');
return Container();
}
}
关于context.bloc<AddBloc>().add(CategoryChange([category]));的问题。是反模式吗?这应该怎么做?
顺便说一句,当它看起来像这样。下次当我将 CategoryChange 事件添加到 bloc(这将产生 CategoryChanged 状态)时,上面的构建器由于某种原因没有反应......
谢谢!
我使用的是 Flutter 1.22.3,flutter_bloc: 6.1.0
【问题讨论】:
标签: flutter dart bloc flutter-bloc