admin管理员组文章数量:1392054
I'm working with Flutter Bloc and noticed that when I dispatch the same event multiple times in quick succession, Bloc emits multiple Loading states before transitioning to Success. This behavior seems expected, but I'm wondering if it's a practical concern in real-world applications.
Bloc code:
sealed class CategoryState {
const CategoryState();
}
final class CategoryInitial extends CategoryState {}
final class CategoryLoading extends CategoryState {}
final class CategorySuccess extends CategoryState {}
final class CategoryFailure extends CategoryState {}
Future<void> _onAddCategory(AddCategoryEvent event, Emitter<CategoryState> emit) async {
emit(CategoryLoading());
try {
await _categoryRepository.addCategory(event.categoryMap);
emit(CategorySuccess());
} catch (e) {
emit(CategoryFailure());
}
}
Triggering Events Rapidly:
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
Observed State Transitions (Log Output):
Transition: { currentState: CategoryInitial, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategorySuccess }
Transition: { currentState: CategorySuccess, event: AddCategoryEvent, nextState: CategorySuccess }
Would this ever happen in a real-world application, or is this just an artificial test case? If this is a concern, what is the best way to prevent redundant state emissions while still ensuring all events are processed?
I'm working with Flutter Bloc and noticed that when I dispatch the same event multiple times in quick succession, Bloc emits multiple Loading states before transitioning to Success. This behavior seems expected, but I'm wondering if it's a practical concern in real-world applications.
Bloc code:
sealed class CategoryState {
const CategoryState();
}
final class CategoryInitial extends CategoryState {}
final class CategoryLoading extends CategoryState {}
final class CategorySuccess extends CategoryState {}
final class CategoryFailure extends CategoryState {}
Future<void> _onAddCategory(AddCategoryEvent event, Emitter<CategoryState> emit) async {
emit(CategoryLoading());
try {
await _categoryRepository.addCategory(event.categoryMap);
emit(CategorySuccess());
} catch (e) {
emit(CategoryFailure());
}
}
Triggering Events Rapidly:
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
bloc.add(AddCategoryEvent(categoryMap: categoryModelMap));
Observed State Transitions (Log Output):
Transition: { currentState: CategoryInitial, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategoryLoading }
Transition: { currentState: CategoryLoading, event: AddCategoryEvent, nextState: CategorySuccess }
Transition: { currentState: CategorySuccess, event: AddCategoryEvent, nextState: CategorySuccess }
Would this ever happen in a real-world application, or is this just an artificial test case? If this is a concern, what is the best way to prevent redundant state emissions while still ensuring all events are processed?
Share Improve this question asked Mar 12 at 4:36 TONI IVANOVTONI IVANOV 634 bronze badges1 Answer
Reset to default 2Yes It is a practical concern in the real world. You can see user can spam the button adding multiple API calls at once. You can also see user typing really fast in the search field.
Bloc gives flexibility on treating these events in the form of transformers.
https://bloclibrary.dev/bloc-concepts/#advanced-event-transformations
Here Transformers allows you to control how events are processed, particularly when multiple events are added in quick succession. The bloc_concurrency package provides pre-built event transformers that help manage event handling efficiently.
Summary of those I know is bellow.
Transformer | Info |
---|---|
sequential() | Default, processes one event at a time |
debounce() | Waits before executing the latest event, Used for Live search, handling rapid user input |
others are restartable(), droppable()
Examples you can see here. https://medium/mindful-engineering/flutter-bloc-transformers-7b62c32d80f6
本文标签: Does Flutter Bloc handle multiple rapid event emissions correctlyStack Overflow
版权声明:本文标题:Does Flutter Bloc handle multiple rapid event emissions correctly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770063a2624285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论