小编Far*_*zam的帖子

NoSuchMethodError: The getter 'length' was called on null

So currently I'm building apps with Flutter which's still new to me, and I'm stuck at those exception in that title.

The problem is when I tried to call "widget.usernames.length" at ListView.builder, it would return those exception if it's null and no exception when there's a data.

我想要完成的是get Length函数应该返回null或0值,以便ListView在没有数据时不显示任何内容。

return new Expanded(
  child: ListView.builder(
      padding: new EdgeInsets.all(8.0),
      itemExtent: 20.0,
      itemCount: widget.usernames.length,
      itemBuilder: (BuildContext context, int index){
        return Text("Bank ${widget.usernames[index] ?? " "}");
      }
  ),
);
Run Code Online (Sandbox Code Playgroud)

我已经试过了

itemCount: widget.usernames.length ?? 0 …
Run Code Online (Sandbox Code Playgroud)

dart flutter

2
推荐指数
1
解决办法
5209
查看次数

标签 统计

dart ×1

flutter ×1