英文:
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) => myItem.myBool)
, or inside the builder, depending on the bool value just return a SizedBox.shrink()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论