optimize two ternary operation in dart

58 views Asked by At

I still haven't found anyway for optimizing this code, I think there should be something missing from the code, please help me to make it shorter.

firstWay:

     fromAddress
                    Get.isDarkMode
                        ? isCart
                            ? Images.emptyCartDark
                            : Images.noDataFoundDark
                        : isCart
                            ? Images.emptyCartLight
                            : Images.noDataFoundLight,

second way:

                    isCart
                        ? Get.isDarkMode
                            ? Images.emptyCartDark
                            : Images.emptyCartLight
                        : Get.isDarkMode
                            ? Images.noDataFoundDark
                            : Images.noDataFoundLight,
1

There are 1 answers

0
WebDesk Solution On

Please create function like below.

String getImage(bool isCart, bool isDarkMode) {
  return Images['${isCart ? 'Cart' : 'NoDataFound'}${isDarkMode ? 'Dark' : 'Light'}'];
}