Skip to content

isClosed is false although the bloc is closed #4749

@ahmedsameha1

Description

@ahmedsameha1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds repro infoThe issue is missing a reproduction sample and/or steps

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions