spebbe / dartz

Functional programming in Dart
MIT License
749 stars 60 forks source link

Async issue when using mаp() #112

Closed ihorkozar closed 1 year ago

ihorkozar commented 2 years ago

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, emit) { future.whenComplete(() => emit(...)); });

GOOD on((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),
        ),
      ),
    );