dartz
dartz copied to clipboard
Async issue when using mаp()
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),
),
),
);