Home > Back-end >  How can I implement this logic without null safety error?
How can I implement this logic without null safety error?

Time:09-21

I'm using getX to navigate to another page if the user account exists.

body: Column(
        children: [
          Center(
            child: Obx(
              () {
                if (accountExist.value) {
                  // return login button UI
                }else{
                  Get.to(ProductsPage());
                }
              },
            ),
          ),
        ],
      ),

Im having the following error:

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.

I want to navigate to the products page if the account exists. How can I implement this without the null error?

CodePudding user response:

body: Column(
        children: [
          Center(
            child: Obx(
              () {
                if (accountExist.value) {
              return Container();
                }else{
                  Get.to(ProductsPage());
                  return Container();
                }
              },
            ),
          ),
        ],
      ),

CodePudding user response:

You can simply return the ProductsPage instead of navigating to it.

body: Obx(
        () {
           if (accountExist.value) {
              // return login button UI     
           } else {
              return ProductsPage();
           }
         },
      ),

CodePudding user response:

Added empty widget to else part.

body: Builder((context){
if(accountExist.value){
 Get.to(ProductsPage());
}
Column(
        children: [
          Center(
            child: Obx(
              () {

                if (accountExist.value) {
                  // return login button UI
                }else{
                  return SizedBox();
                }
              },
            ),
          ),
        ],
      ),

CodePudding user response:

Remove Get.to(); and use only widget in obx

body: Column(
        children: [
          Center(
            child: Obx(
              () {
                if (accountExist.value) {
                  // return login button UI
                }else{
                  ProductsPage();
                }
              },
            ),
          ),
        ],
      ),
  • Related