Either inside BlocProvider is not returning value


I want to use Either to store result in BlocBuilder. According to result I want to show specific Widget. But using Either will throw exception that BlocBuilder returned null. I debugged that code and fold branch was called correctly, so it should return Widget. I don’t understand how it could return null.


else if (state is Loaded) {
  final badgeOrFailure = state.profile.getBadgeByOrgId(orgId);
  badgeOrFailure.fold((err) {
       return MessageDisplay(
         message: err.message,
  }, (badge) {
      return BadgeWidget(
       desc: badge.desc,
       code: badge.code,

Profile code:

Either<BadgeNotFoundFailure, Badge> getBadgeByOrgId(int orgId) {
    try {
      if (badges != null && badges.isNotEmpty) {
        return Right(badges.firstWhere((element) => element.orgId == orgId));
      } else {
        log('badges are empty');
        return Left(BadgeNotFoundFailure());
    } on Exception catch (_) {
      return Left(BadgeNotFoundFailure());


I think that you need an explicit return, for example
return badgeOrFailure.fold(...

Can you please try it?

Answered By – Yilmaz Guleryuz

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *