【问题标题】:How to use item count in ListView.Builder?如何在 ListView.Builder 中使用项目计数?
【发布时间】:2020-02-26 14:43:24
【问题描述】:

Click Here for Image!Click here to see error when i input 'itemCount'我想问一下,我在使用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)),
                ),
              );
            },
          )))),

在这里我称之为“类别”和“项目”。我将它包装在一个包装小部件中。然后对于类别,我确实将其分为 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 {



                                                    ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您正在访问allGames["games"],它表示您正在尝试在null 上使用[]

    似乎allGamesnull,所以请注意这一点。

    除此之外,您使用itemCount 没问题!

    您可以通过执行以下操作来添加一些 null 意识:

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

    【讨论】:

    • 谢谢你的帮助
    【解决方案2】:

    我认为当 build 方法第一次运行时,你的 allGames 为空。所以,尝试添加一个默认值,例如:

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

    【讨论】:

    • 这会崩溃,因为 allGames 为 null 并且下标运算符将应用于 null。
    • 谢谢你的帮助
    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2021-08-06
    • 2021-12-26
    • 2022-10-24
    • 2019-10-30
    • 2021-01-19
    相关资源
    最近更新 更多