enjoycode
enjoycode

Reputation: 240

How to use item count in ListView.Builder?

Click Here for Image! Click here to see error when i input 'itemCount' I want to ask, I am having trouble using the List.builder () feature. where I can't limit the data contained in the list so that it will display an error message about the error range. Here is the source code that I gave.

  _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)),
                ),
              );
            },
          )))),

and here i call the 'Category' and 'Item'. I wrap it in a wrap widget. then for categories, I did divide it into 3 parts. then every part in the category I named 'items' and the problem is the items here have excess capacity so that the data displayed is leftover and not on target.

                                                            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 {



                                                    ),

Upvotes: 0

Views: 8494

Answers (2)

magicleon94
magicleon94

Reputation: 5172

You're accessing allGames["games"] and it says that you're trying to use [] on null.

Seems like allGames is null, so put an eye on that.

Other than that, your use of itemCount is fine!

You can add some null awareness by doing something like this:

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

Upvotes: 1

Sergei Mikhailovskii
Sergei Mikhailovskii

Reputation: 2465

I think when the build method runs at the first time, your allGames is null. So, try to add a default value, for example:

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

Upvotes: 0

Related Questions