dartz icon indicating copy to clipboard operation
dartz copied to clipboard

Async issue when using mаp()

Open ihorkozar opened this issue 3 years ago • 0 comments

Unhandled Exception: 'package:bloc/src/emitter.dart': Failed assertion: '!_isCompleted'

emit was called after an event handler completed normally. This is usually due to an unawaited future in an event handler. Please make sure to await all asynchronous operations with event handlers and use emit.isDone after asynchronous operations before calling emit() to ensure the event handler has not completed.

BAD on<Event>((event, emit) { future.whenComplete(() => emit(...)); });

GOOD on<Event>((event, emit) async { await future.whenComplete(() => emit(...)); });

failureOrToken.map((token) async {
        final failureOrProfile =
            await profileRepository.getCurrentUserProfile(token);
        failureOrProfile.map(
          (profile) async {
            final failureOrTeams = await teamsRepository.getTeams(token);
            failureOrTeams.map(
              (teams) async {
                final failureOrLocations =
                    await locationsRepository.getLocations(token);
                failureOrLocations.map(
                  (locations) => emit(
                    SuccessState(
                      profile: profile,
                      teams: teams,
                      locations: locations,
                    ),
                  ),
                );
              },
            );
          },
        );
      },
    ).leftMap(
      (failure) => emit(
        ErrorState(
          message: mapFailureToMessage(failure),
        ),
      ),
    );

ihorkozar avatar Aug 03 '22 12:08 ihorkozar