在 gridview 中加载大量数据时应用程序崩溃

App crashes when load a lot of data inside a gridview

描述

我正在使用 gridview 来显示市场的产品,当滚动 gridview 九次时我的应用程序崩溃了。

此屏幕的小部件构建return SmartRefresher 包装了 GridView。

return SmartRefresher(
      controller: controller.refreshController,
      onLoading: controller.onLoading,
      onRefresh: controller.onRefresh,
      enablePullUp: true,
      child: Observer(builder: (context) {
        return GridView.builder(
          padding: EdgeInsets.only(top: 10),
          shrinkWrap: true,
          cacheExtent: 100,
          physics: ScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount:
                MediaQuery.of(context).orientation == Orientation.portrait
                    ? 2
                    : 3,
            crossAxisSpacing: 5.0,
            mainAxisSpacing: 5.0,
            childAspectRatio: 0.85,
          ),
          itemCount:
              controller.produtos == null ? 0 : controller.produtos.length,
          itemBuilder: (context, index) {
            Produto produto = controller.produtos[index];

            //here I return my card
          },
        );
      }),
    );

注意:如果删除 SmartRefresher,我可以滚动包含大量数据的 GridView。

尝试将 Observer 放在 SmartRefresher 上方,如下所示:

Observer(
  builder: (context) {
    return SmartRefresher(
      controller: controller.refreshController,
      onLoading: controller.onLoading,
      onRefresh: controller.onRefresh,
      enablePullUp: true,
      child: GridView.builder(
        padding: EdgeInsets.only(top: 10),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount:
              MediaQuery.of(context).orientation == Orientation.portrait
                  ? 2
                  : 3,
          crossAxisSpacing: 5.0,
          mainAxisSpacing: 5.0,
          childAspectRatio: 0.85,
        ),
        itemCount:
            controller.produtos == null ? 0 : controller.produtos.length,
        itemBuilder: (context, index) {
          Produto produto = controller.produtos[index];

          
        },
      ),
    );
  },
);