Home > Enterprise >  How to set null case in Gridview builder
How to set null case in Gridview builder

Time:03-16

I am trying to create a container and write no data found in case the data coming from the API is null.

Although I deleted the link of the API, the container does not appear.

 body: Obx(
        () => fGames2SecScreenLoad.isLoading.value
            ? const Center(
                child: LoadingWidget(),
              )
            : GridView.builder(
                shrinkWrap: true,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  childAspectRatio: (itemWidth / itemHeight),
                  crossAxisCount: 1,
                ),
                scrollDirection: Axis.vertical,
                itemCount: freeGameTypeController.freeGames.length,
                itemBuilder: (BuildContext context, int index) {
                  
                  var freeGames = freeGameTypeController.freeGames[index];
                  if(freeGamesController.freeGamesList.isEmpty){
                   return Container(chid: Text("No data found");}
                 else{
                  return Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: 7.w,
                      vertical: 8.h,
                    ),
                    child: GestureDetector(
                      onTap: () {
                        Get.to(
                          const FreeGamesIndex(),
                          transition: Transition.native,
                          duration: const Duration(milliseconds: 200),
                          arguments: freeGames,
                        );
                      },
                      child: FreeGamesListCard(
                        fImage: freeGames.image,
                        fTitle: freeGames.title,
                        fPlatforms: freeGames.platforms,
                        fWorth: freeGames.worth,
                      ),
                    ),
                  );
                }}),
      ),

CodePudding user response:

Try this:

 body: Obx(
   () => fGames2SecScreenLoad.isLoading.value
       ? const Center(
           child: LoadingWidget(),
        )
        : Builder(
            builder: (_){
             if(freeGamesController.freeGamesList.isEmpty){
                   return Container(chid: Text("No data found");
              }
             return  GridView.builder(
                shrinkWrap: true,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  childAspectRatio: (itemWidth / itemHeight),
                  crossAxisCount: 1,
                ),
                scrollDirection: Axis.vertical,
                itemCount: freeGameTypeController.freeGames.length,
                itemBuilder: (BuildContext context, int index) {
                  
                  var freeGames = freeGameTypeController.freeGames[index];
                  return Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: 7.w,
                      vertical: 8.h,
                    ),
                    child: GestureDetector(
                      onTap: () {
                        Get.to(
                          const FreeGamesIndex(),
                          transition: Transition.native,
                          duration: const Duration(milliseconds: 200),
                          arguments: freeGames,
                        );
                      },
                      child: FreeGamesListCard(
                        fImage: freeGames.image,
                        fTitle: freeGames.title,
                        fPlatforms: freeGames.platforms,
                        fWorth: freeGames.worth,
                      ),
                    ),
                  );
                }),
  
        ),
       }),
      ),
  • Related