felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.84k stars 3.4k forks source link

fix: Unhandled Exception: 'package:bloc/src/bloc.dart': Failed assertion: line 232 pos 7: '!_isCompleted': #3098

Closed gilangsatria09 closed 2 years ago

gilangsatria09 commented 2 years ago

Description I tried called emit inside dartz fold after i called await function

var token = await fetchToken(NoParams()); token.fold((l) async => emit(ErrorState('error')), (r) async { //Error Unhandled Exception if this line added var res = await getBanner(GetBannerParams(token: r));

  emit(GetHomeDataSuccess(listBanner: []));

});

Expected Behavior I expected that emit(GetHomeDataSuccess) is called successful

Screenshots image image

felangel commented 2 years ago

Hi @gilangsatria09 👋 Thanks for opening an issue!

You need to await the call to token.fold to prevent the event handler from completing before emit is called.

Let me know if that helps! 👍

gilangsatria09 commented 2 years ago

Thank you so much for reply my issues this fast!!

so, what i should do is add await to the token.fold like this: var token = await fetchToken(NoParams()); await token.fold((l) async => emit(ErrorState('error')), (r) async {

am i right?

felangel commented 2 years ago

Thank you so much for reply my issues this fast!!

so, what i should do is add await to the token.fold like this: var token = await fetchToken(NoParams()); await token.fold((l) async => emit(ErrorState('error')), (r) async {

am i right?

Yup, no problem! 👍 Let me know if that resolves the issue.

gilangsatria09 commented 2 years ago

omg!!! thank you soooooo much, it really worked. thank you for helping me despite my bad english

felangel commented 2 years ago

omg!!! thank you soooooo much, it really worked. thank you for helping me despite my bad english

No problem! 😄