创建一个ListView.Builder,使用列表中的布尔值为true。Flutter

huangapple go评论64阅读模式
英文:

create ListView.Bulder from true bools in a list. Flutter

问题

我有一个名为MyList的列表,其中包含一个唯一的类<MyItem>。在MyItem类中,有一个字符串和一个布尔值。

我有一个ListView.Builder,我希望它从MyList创建一个瓷砖列表,但我只希望它为MyItem中的布尔值为true的项目创建瓷砖。

请问需要哪种语法来实现这个,请问?

非常感谢。

英文:

I have a list called MyList which takes a unique class called <MyItem>. in MyItem class, there is a String and a bool.

I have a ListView.Builder and I want it to create a list of tiles from MyList but i only want it to create tiles for where the bool in MyItem is true.

what is the syntax needed to do this please?

thanks so much

答案1

得分: 1

你可以像这样筛选列表:final myFilteredList = myList.where((myItem) => myItem.myBool),或者在构建器内根据布尔值返回SizedBox.shrink()

英文:

You can either filter the list likefinal myFilteredList = myList.where((myItem) =&gt; myItem.myBool), or inside the builder, depending on the bool value just return a SizedBox.shrink().

huangapple
  • 本文由 发表于 2023年7月28日 04:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定