我是新手程序员,
在itembuilder
中如何跳到下一个索引项目?
return ListView.builder(
itemCount: s.data.length,
itemBuilder: (_, index){
if(s.data[index]["count"].toString() == "0") return new Text('');
image1 = s.data[index]["image"];
if(image1 == null){
image2 = imageSrc;
}else{
image2 = s.data[index]["image"]["src"];
print(s.data[index]);
}
我用过
if(s.data[index]["count"].toString() == "0") return new Text('');
但是,项目之间的空间已创建。
我假设您想跳过某些项目。如果您使用的是Dart 2.3+,则可以使用collection if
或collection for
执行此操作。但是,在您的用例中,最简单的方法是将Text('')
替换为const SizedBox()
。小部件树中将有一个小部件,但高度/宽度为0时,它将不会显示在屏幕上并占用任何空间,并且作为const
小部件,不应重新构建。
假设我已正确理解,您想跳过count
为零的项目的构建。
保持该代码,您可以返回SizedBox.shrink()
而不是Text('')
,它是一个零大小的容器。
更好的方法是过滤器s.data
,并在ListView.builder
中使用它:
List<?> filteredData = s.data.where((item) => item["count"].toString() != 0).toList();
?
代表数据类型。
我不知道您为什么使用toString
,如果count
是整数,则更好的方法是:
List<?> filteredData = s.data.where((item) => item["count"] != 0).toList();
然后在s.data
中将filteredData
替换为ListView.builder