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 {
),
答案 0 :(得分:1)
您正在访问allGames["games"]
,它表明您正在尝试在[]
上使用null
。
好像allGames
是null
,所以请注意这一点。
除此之外,您可以使用itemCount
!
您可以通过执行以下操作来添加一些空意识:
itemCount = allGames != null ? (allGames["games"]?.length ?? 0) : 0
答案 1 :(得分:0)
我认为当build方法第一次运行时,您的allGames为null。因此,尝试添加默认值,例如:
itemCount: allGames['games'].length ?? 0