Ad

App Crashes When Load A Lot Of Data Inside A Gridview

Description

I'm using a gridview to show the products of a market and when a scroll the gridview nine times my app crashes.

The widget build of this screen return a SmartRefresher wrapped a 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
          },
        );
      }),
    );

Note: If I remove the SmartRefresher I can scroll the GridView with a lot of data.

Ad

Answer

Try to place the Observer above SmartRefresher, like this:

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];

          
        },
      ),
    );
  },
);
Ad
source: stackoverflow.com
Ad