I have a question about bloc correct code placement so to say.
So as bloc stands for business logic component I've moved business logic (like processing data methods) inside class. So now there are 2 methods one for getting and second for saving data.
Next, I have my component which within AddInitial
should show loading, then fire bloc saying "hey, I need data here". Is it correct or it's antipattern? Code looks like this.
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();
}
}
Question about context.bloc<AddBloc>().add(CategoryChange([category]));
. Is it antipattern? How this should be done?
By the way when it looks like this. Next time when I add CategoryChange
event to bloc (which will yields CategoryChanged
state) builder from above doesn't react for some reason...
Thanks!
I'm using Flutter 1.22.3, flutter_bloc: 6.1.0