Can I simplify a condition with short-circuit call chain in Dart?

730 Views Asked by At

Can this code snippet be simplified on the second line?

From:

GetBuilder<ProductController>(builder: (productController) {
    return productController.reviewedProductList == null || productController.reviewedProductList.length != 0
    ? CategoryDrinksView(productController: productController, isPopular: true)
    : SizedBox();
})

To:

GetBuilder<ProductController>(builder: (productController) {
    return productController.reviewedProductList?.length != 0
    ? CategoryDrinksView(productController: productController, isPopular: true)
    : SizedBox();
})

The IDE doesn't return any errors and everything seems to work, but I would like to know if this is common in Dart and can be considered good practice.

1

There are 1 best solutions below

1
Abdallah Abdel Aziz On

I don't think it's a good idea. You may get a 'NoSuchMethodError: The getter 'length' was called on null' error, if the value of reviewedProductList was null. So keep the double checking