Ahmad Raza
Ahmad Raza

Reputation: 858

Value Not Updating in BlocBuilder and Bloc Consumer

I have list in my HomeScreenState i want to update list item based on index but it is not updating on UI instalnstaly i have to rebuild the state in order to see the change, i try using BlocConsumer and BlocBuilder but none work for me.

This is my state part of 'home_cubit.dart'; class HomeScreenState extends Equatable{

  List<HomePostEntity> trendingPostList = [];
  ScreenStatus trendingScreenStatus;
  String errorMessage;
  String likeDislikeErrorMessage;

   HomeScreenState({
     required this.trendingScreenStatus,
     required this.trendingPostList,
     required this.errorMessage,
     required this.likeDislikeErrorMessage,
});

  HomeScreenState copyWith({
    ScreenStatus? trendingScreenStatus,
    List<HomePostEntity>? trendingPostList,
    String? errorMessage,
    String? likeDislikeErrorMessage,
  }) {
    return HomeScreenState(
      trendingScreenStatus: trendingScreenStatus ?? this.trendingScreenStatus,
      trendingPostList: trendingPostList ?? this.trendingPostList,
      errorMessage: errorMessage ?? this.errorMessage,
      likeDislikeErrorMessage: likeDislikeErrorMessage ?? this.likeDislikeErrorMessage,
    );
  }
  @override
  List<Object?> get props => [trendingScreenStatus,trendingPostList,errorMessage,likeDislikeErrorMessage];
}

This is my cubit

updateLikeCount({required List<HomePostEntity> listToUpdate,required int index,required bool increase, required ListType listType}) {


        List<HomePostEntity> updatedCatList = List.from(state.trendingPostList);
        int newCount = updatedCatList[index].likesCount! + 1;
        updatedCatList[index].likesCount = newCount;
        emit(state.copyWith(trendingPostList: updatedCatList,count: state.count++));

  }

This is how i am using BlocBuilder

BlocBuilder<HomeScreenCubit, HomeScreenState>(
        builder: (_, state) {

}

and this is where i want to reflect the change

 Text(state.trendingPostList[index].likesCount.toString(),
                                    style: const TextStyle(color: AppColor.white)

Upvotes: 0

Views: 87

Answers (1)

Namatullah Wahidi
Namatullah Wahidi

Reputation: 17

Use equatable package or override == and hashCode for your state class to compare object in your bloc state because if the object is the same, the bloc will not emit the state

Upvotes: 0

Related Questions