Home > Enterprise >  How to conditionally show Get.bottomSheet when starting app
How to conditionally show Get.bottomSheet when starting app

Time:08-25

I am trying to popup Get.bottomSheet when starting app.

I did it like bottom code.

@override
void initState() {
  _popUpBottomBanner();
}

void _popUpBottomBanner() {
  WidgetsBinding.instance.addPostFrameCallback(
    (_) {
      Get.bottomSheet(
        ...
      );
    },
  );
}

But I want to judge show or hide bottom sheet by API result.

So I changed Code like below.

void _popUpBottomBanner() {
  WidgetsBinding.instance.addPostFrameCallback(
    (_) {
      FutureBuilder<CustomListVO>(
        future: Api().getBanners(...),
        builder: (BuildContext _, AsyncSnapshot<CustomListVO> snapshot) {
          if (snapshot.hasError) return;
          if (!snapshot.hasData) return;
          if (snapshot.data?.list?.isNotEmpty == true) {
            Get.bottomSheet(
              ...
            );
          }
          return;
        },
      );
    },
  );
}

Despite confirming API result arriving properly,

bottomSheet isn't showing.

What is the problem with this code?

Thanks for reading :D

====== resolved like below ========

@override   
Widget build(BuildContext context) {     
  return FutureBuilder<CustomListVO>(         
    future: Api().getBanners(...),         
    builder: (BuildContext _, AsyncSnapshot<CustomListVO> snapshot) {           
      if (snapshot.data?.list?.isNotEmpty == true) {             
        Get.bottomSheet(               
          ...             
        );           
      }
      return HomePage();
    }
  );
}

CodePudding user response:

you need to show bottom sheet that is not required to use Futurebuilder so you can use it like this :

var result = await Api().getBanners(...);
if (result.data?.list?.isNotEmpty == true) {
        Get.bottomSheet(
          ...
        );
      }
  • Related