skip to Main Content

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];
}

2

Answers


  1. Make all the lists growable: false in your state class. So when you are creating a state object, pass the list as follows.

    List.from([1,2,3], growable: false);

    now, you won’t be able to add any elements to same list object. Every time, you will have to do List.from([...oldList, ...newItems], growable:false);

    This will make sure your data is immutable and state is emitted.

    Login or Signup to reply.
  2. 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, ...);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search