gpt4 book ai didi

flutter - flutter 如何按1值过滤数据?

转载 作者:行者123 更新时间:2023-12-03 04:23:37 26 4
gpt4 key购买 nike

我需要过滤应用程序中的数据,但它显示的错误是我的工作方式。

现在它需要打印[Instance of 'Post'],我需要打印所有值

class _FilterScreenState extends State<FilterScreen> {

List showPost;

@override
void initState(){
super.initState();
print(widget.id);
showPost = posts.where((i) => i.id == widget.id).toList();
print(showPost);

}

我需要过滤我的 id等于我的 widget.id的数组

在其他文件上,数据如下所示
final List<Post> posts = [
Post(
id: 0,
authorName: 'Umaiz Khan',
authorImageUrl: 'assets/images/user0.png',
timeAgo: '5 min',
imageUrl: 'assets/images/post0.jpg',
),
Post(
id: 1,
authorName: 'Saad ahmed',
authorImageUrl: 'assets/images/user1.png',
timeAgo: '10 min',
imageUrl: 'assets/images/post1.jpg',
),
Post(
id: 2,
authorName: 'Hiba',
authorImageUrl: 'assets/images/user4.png',
timeAgo: '10 min',
imageUrl: 'assets/images/post2.jpg',
),
];

最佳答案

您可以覆盖Post类的toString()方法,如下所示

class Post{
int id;
String authorName;
String authorImageUrl;
String timeAgo;
String imageUrl;
Post({this.id,this.authorName,this.authorImageUrl,this.timeAgo,this.imageUrl});


@override
String toString( ) {
return 'Post{id: $id, authorName: $authorName, authorImageUrl: $authorImageUrl, timeAgo: $timeAgo, imageUrl: $imageUrl}';
}

}

关于flutter - flutter 如何按1值过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com