我试图用相同的状态发出不同的数据集,使用相等。但是,当接下来的5个记录被添加到列表中时,状态不会在第二次被释放。
如果有人能帮忙就太好了。
我就是这样发帖子的:
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);这是国家阶级:
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];
}发布于 2022-11-29 05:06:24
更改您的模型如下,以您的时间戳为int,并分配唯一的时间戳值(Mili-秒),每次你发出。
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];
}有这样的价值:
LoadedPosts(now: DateTime.now().millisecondsSinceEpoch, ...);发布于 2022-11-29 04:55:12
使所有列表都可以增长:在您的状态类中为false。因此,当您创建一个state对象时,按如下方式传递列表。
List.from([1,2,3], growable: false);
现在,您将无法向同一个list对象添加任何元素。每次你都要做List.from([...oldList, ...newItems], growable:false);
这将确保数据是不可变的,并发出状态。
https://stackoverflow.com/questions/74601287
复制相似问题