Search code examples
flutterdartflutter-getx

How can I implement this logic without null safety error?


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?


Solution

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

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