Search code examples
flutterbloc

Unable to emit same state when new data gets added using equatable in flutter bloc


I am trying to emit same state with different set of data using equatable. But, somehow the state doesn't get emitted the second time when next 5 records are added in the list.

It would be great if someone could help.

This is how I am emitting post:

loadedState = LoadedPosts(
                      now: DateTime.now(),
                      post: List.from(postDetailsFilteredPostResponse, growable: false),
                          newCount: 0,
                          friends: List.from(postFriendsResponse, growable: false),
                          likes: List.from(postLikesResponse, growable: false),
                          comments:List.from(postCommentsResponse, growable: false),
                          photos: List.from(postPhotosResponse, growable: false),
                          userDetail: userDetail);

emit(loadedState);

This is the state class:

 abstract class PostState extends Equatable{
    @override
    List<Object?> get props => [];
}
class LoadedPosts extends PostState{
  List<Post> post = List<Post>.filled(5, Post(postId: ''));
  final List<User>? friends;
  final List<Images> photos;
  final List<UserLikes> likes;
  final List<UserComments> comments;
  final User? userDetail;
  final int newCount;
  final DateTime now;


  LoadedPosts({
    required this.post,
    required this.friends,
    required this.photos,
    required this. likes,
    required this.comments,
    required this.newCount,
    required this.now,
    this.userDetail });
 

  @override
  List<Object?> get props => [now, post];
}

Solution

  • Change your Model as below, have your timestamp as int and assign unique timestamp value ( Mili-seconds ) every-time when you emit.

    abstract class PostState extends Equatable {
        @override
        List<Object?> get props => [];
    } 
    
    class LoadedPosts extends PostState {
        final List<Post> post;
        final List<User>? friends;
        final List<Images> photos;
        final List<UserLikes> likes;
        final List<UserComments> comments;
        final User? userDetail;
        final int newCount;
        final int now; // have this as ( int )
    
    
    LoadedPosts({
        required this.post,
        required this.friends,
        required this.photos,
        required this. likes,
        required this.comments,
        required this.newCount,
        required this.now,
        this.userDetail });
    
    
        @override
        List<Object?> get props => [now, post];
    }
    

    Have the value like this:

    LoadedPosts(now: DateTime.now().millisecondsSinceEpoch, ...);