Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振列表视图在itemCount参数中获取一些项_Flutter_Dart - Fatal编程技术网

Flutter 颤振列表视图在itemCount参数中获取一些项

Flutter 颤振列表视图在itemCount参数中获取一些项,flutter,dart,Flutter,Dart,正如我们所知,ListView小部件中有itemCount,我在数组中有100多个项目,我只想从中提取10个项目,例如: itemCount: _dashboardViewModel.media.where((element) => element.type=='images').length, 在长度为100的情况下,如何在itamCount参数中仅获取10项 child: Container( padding: const EdgeInsets.only(left: 10.0,

正如我们所知,ListView小部件中有itemCount,我在数组中有100多个项目,我只想从中提取10个项目,例如:

itemCount: _dashboardViewModel.media.where((element) => element.type=='images').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,
          ),
        ),
      );
    },
  ),
),
尝试使用sublist方法

像这样

itemCount: _dashboardViewModel.media.where((element) => element.type=='images').toList().sublist(0, 10).length,

我建议您使用,这有助于您获得所需的前n个数字在本例中,前10个数字

toList对于这个方法不是强制性的,所以从代码中删除它仍然会给出长度

指针:如果出于某种原因需要特定的列表对象类型,那么可能需要使用toList。但在这种情况下不是这样

itemCount: _dashboardViewModel.media.where((element) => element.type=='images').take(10).length

根据什么只有10个?前10名?最后10项?为什么不创建一个包含要显示的元素的新列表或数组,然后显示该列表?@从第一项开始锁定您说您需要10项,那么是10项呢?你想从100个项目列表中选择这10个项目的条件是什么?@Abbas.M他们已经指定了。我相信你忘记了。长度,我也不知道这与硬编码10有什么不同,如果他已经知道他想要的正好是10,或者在列表大小和10之间留出一分钟,以避免列表太大时出现错误small@Abbas.M长度不是一种方法。这是一个很好的办法。我同意硬编码的说法。@christophemore你是对的,我编辑了它。这里的toList是完全不必要的。我同意你的说法@christophemore。但我们无论如何都可以使用它,它会将数据返回到列表项中。比如[1,2,3]。如果没有定义:它将是这样的1,2,3,基本上在集合中。没什么区别。谢谢你为我指出这一点:没什么意义。这是一个没有目的的额外操作。编辑代码@Christophermore谢谢: