flutter - Flutter ListView 在 itemCount 参数中获取一些项目
问题描述
正如我们所知,我们itemCount
在ListView
小部件中有 100 多个项目,我只想从中获取 10 个项目,例如:
itemCount: _dashboardViewModel.media.where((element) => element.type=='images').length,
那length
是 100,我怎么能只取itamCount
参数中的 10 项?
child: Container(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: ListView.separated(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return ...;
},
itemCount: _dashboardViewModel.media.where((element) => element.type=='images').length,
separatorBuilder: (context, index) {
return Center(
child: Container(
width: 6.0,
height: 6.0,
margin: const EdgeInsets.only(top: 20, right: 5.0, left: 5.0, bottom: 15.0),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.1),
shape: BoxShape.circle,
),
),
);
},
),
),
解决方案
我建议你使用take(),它可以帮助你在这种情况下获得你想要的前 n 个数字,first 10 numbers
toList()
这种方法不是强制性的,所以从代码中删除它仍然会给出长度。
指针:如果您List
出于某种原因专门需要一个对象类型,那么您可能想要使用toList()
. 但在这种情况下不是。
itemCount: _dashboardViewModel.media.where((element) => element.type=='images').take(10).length
推荐阅读
- r - 基于样本量的变化因子
- r - 在 R 包中添加数据集
- javascript - TypeError: response.hits[i].hit_set_id.toLowercase 不是函数
- go - 带有 Go 扩展的 VSCODE:如何使用键盘快捷键选择第二个或第三个代码完成提示?
- python - 在python的二叉搜索树中插入一个节点
- microsoft-graph-api - 无法调用 PSTN microsoft graph API
- cmake - open62541 库在 Windows 上构建,带有发布文件
- python - 将对象从一个列表链接到另一个列表并删除一个新列表
- python-3.x - 如何从 nltk 分类器中获取 pov/neg 百分比?
- patch - 修补程序在 ghidra 上添加新打印