gpt4 book ai didi

list - 在包含特定名称的列表中显示项目 - flutter

转载 作者:IT王子 更新时间:2023-10-29 07:11:40 27 4
gpt4 key购买 nike

在此列表中,我想显示包含此特定名称的所有项目。

我的列表项:['US', 'SG', 'US']

print(list.contains("US"));

使用 .contains() 返回 true 或 false,但它不会返回包含它的字符串列表。我只想从列表中提取具有“US”的项目。在这种情况下,有 2 个。请帮忙!

最佳答案

你可以试试下面的方法-

List<String> myList = ['US', 'SG', 'US'];
print(myList.where((item) => item.contains("US")));

您还可以通过以下方式将其直接显示在 Text 小部件中 -

Text(
myList.where((item) => item.contains("US")).join(" "),
//The join function joins the elements of the Iterable into a single string with the separator provided as an argument.
),

希望这对您有所帮助!

更新:

要将每个单词单独显示为列表,您可以按以下方式将它们显示在 Column 中 -

Column(
children: myList.map((value) {
if(value.contains("US")){
return Text(value,);
} else {
return Container();
//Return an empty Container for non-matching case
}
}).toList(),
)

如果您希望 ListView 可以滚动,则可以在 Column 中使用同样的东西。

关于list - 在包含特定名称的列表中显示项目 - flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56033493/

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