Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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

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 如何在ListView.Builder中使用项目计数?_Flutter_Dart - Fatal编程技术网

Flutter 如何在ListView.Builder中使用项目计数?

Flutter 如何在ListView.Builder中使用项目计数?,flutter,dart,Flutter,Dart,我想问一下,我在使用List.builder()功能时遇到了问题。我无法限制列表中包含的数据,因此它将显示有关错误范围的错误消息。这是我给出的源代码 _sort = <Sort>[ Sort( category: 'By Games', item: new Container( height: 200, child: new Container( child: ListView.bui

我想问一下,我在使用List.builder()功能时遇到了问题。我无法限制列表中包含的数据,因此它将显示有关错误范围的错误消息。这是我给出的源代码

  _sort = <Sort>[
  Sort(
      category: 'By Games',
      item: new Container(
          height: 200,
          child: new Container(
              child: ListView.builder(
            scrollDirection: Axis.vertical,
            //itemCount: allGames['games'].length,
            itemBuilder: (context, index) {
              return Container(
                child: Padding(
                  padding: const EdgeInsets.all(15),
                  child: Text(allGames['games'][index]['game_title'].toString(), style: new TextStyle(color: Colors.white)),
                ),
              );
            },
          )))),
\u排序=[
分类(
类别:"按游戏",,
项目:新集装箱(
身高:200,
子容器:新容器(
子项:ListView.builder(
滚动方向:轴垂直,
//itemCount:allGames['games']。长度,
itemBuilder:(上下文,索引){
返回容器(
孩子:填充(
填充:常量边集。全部(15),
子项:文本(allGames['games'][索引]['game_title'].toString(),样式:新文本样式(颜色:Colors.white)),
),
);
},
)))),
这里我称之为“类别”和“项目”。我将其包装在一个包装小部件中。然后对于类别,我将其分为3个部分。然后,我将类别中的每个部分命名为“项目”,问题是这里的项目容量过大,因此显示的数据是剩余的,而不是目标数据

                                                            child: Wrap(
                                                            children: <Widget>[
                                                              ListView.builder(
                                                                shrinkWrap: true,
                                                                itemCount: 3,
                                                                itemBuilder: (context, index) {
                                                                  context = context;
                                                                  if (index < 2) {
                                                                    final sort = _sort[index];
                                                                    return ExpansionTile(
                                                                      title: ListTile(
                                                                        title: Text(
                                                                          sort.category,
                                                                          style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                                                                        ),
                                                                      ),
                                                                      children: <Widget>[
                                                                        ListTile(
                                                                          title: sort.item,
                                                                        )
                                                                      ],
                                                                    );
                                                                  } else {



                                                    ),
子对象:换行(
儿童:[
ListView.builder(
收缩膜:对,
物品计数:3,
itemBuilder:(上下文,索引){
上下文=上下文;
如果(指数<2){
最终排序=_排序[索引];
返回扩展文件(
标题:ListTile(
标题:正文(
分类,
样式:TextStyle(颜色:Colors.white,fontwweight:fontwweight.bold),
),
),
儿童:[
列表砖(
标题:sort.item,
)
],
);
}否则{
),

您正在访问
所有游戏[“游戏”]
,它表示您正试图在
上使用
[]

似乎
allGames
null
,所以请注意这一点

除此之外,您可以使用
itemCount

您可以通过如下操作添加一些空感知:

itemCount = allGames != null ? (allGames["games"]?.length ?? 0) : 0

我认为当build方法第一次运行时,您的allGames为null。因此,请尝试添加默认值,例如:

itemCount: allGames['games'].length ?? 0

这将崩溃,因为allGames为null,并且下标运算符将应用于null。谢谢它会有帮助谢谢它会有帮助