-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Description
I have a page with its own bloc instance. The bloc has the following code:
Future<void> _onSubscriptionRequested(
EntriesSubscriptionRequested event,
Emitter<EntriesState> emit,
) async {
emit(state.copyWith(status: EntriesStatus.loading));
try {
await emit.onEach<EntriesPageData>(
_watchEntriesUsecase.call(event.fieldListId),
onData: (entriesPageData) {
if (!isClosed) {
add(NewData(entriesPageData));
add(PrepareTab(state.currentTabIndex, DateTime.now()));
}
},
onError: (_, _) => emit(state.copyWith(status: EntriesStatus.failure)),
);
} catch (e) {
emit(state.copyWith(status: EntriesStatus.failure));
}
}When I click the FloatingActionButton, a new page is opened. The new page adds new data, and the onData of the previous page is called as the stream emitted the new data. But Bad state: Cannot add new events after calling close is thrown, although the if (!isClosed) check!
Expected Behavior
Bad state: Cannot add new events after calling close should not be thrown. Altought this would mean that the event for new data would not be added!
Additional Context
I add a new event instead of updating the state directly or using methods, because I need to test my bloc when new data is emitted by the stream after the first one. In the todo example, I find how to test the first emitted data, but no tests for the second and the next emitted data. In the tests, I use the act function to add an event and test the expected state.