GridView.builder创建无限滚动的页面

时间:2017-12-14 22:57:44

标签: dart flutter

尝试使用API​​构建网格。一切都很好,但在最后一排瓷砖之后,页面变得空白。只是保持滚动和放大滚动& ...网格是这样构建的:

body: new GridView.builder(
      gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: (orientation == Orientation.portrait) ? 2 : 3),
      itemBuilder: (BuildContext context, int index) {
        return new Card(
          child: new GridTile(
            footer: new Text(data[index]['name']),
              child: new Text(data[index]['image']), //just for testing, will fill with image later
          ),
        );
      },
  )

异常,因为我不断向下滚动空白页面,最后一个数字(包括:24)变大2倍(24,26,28等)的倍数。

I/flutter (11001): Another exception was thrown: RangeError (index): Invalid value: Not in range 0..23, inclusive: 24

有人在GridView.builder中看到过这种行为吗?

1 个答案:

答案 0 :(得分:12)

您可以将项目计数传递给构建器。

示例:

    body: new GridView.builder(
          itemCount: data.length
          gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: (orientation == Orientation.portrait) ? 2 : 3),
          itemBuilder: (BuildContext context, int index) {
            return new Card(
              child: new GridTile(
                footer: new Text(data[index]['name']),
                  child: new Text(data[index]['image']), //just for testing, will fill with image later
              ),
            );
          },
    )

希望有所帮助!