In my code, I'm experimenting with the optional chaining operator in this scenario:
productPrice1: product.prices["1"]?.amount.amount_1,
By using the question mark as shown above, I ensure that if there is no child object with key ["1"], it won't result in an error. However, now I am looking to extend this feature to also handle cases where the parent object prices does not exist.
The prices object contains child objects with keys like "1", "2", and "3. My goal is to first verify if prices is null, and then only check if ["1"] exists if prices itself is defined.
I initially tried to implement this logic but encountered issues:
productPrice1: product.prices?["1"]?.amount.amount_1,
I want to find a way around explicitly checking for null and wondering if there's a better approach than what I have done here:
productPrice1: product.prices != null ? product.prices?["1"]?.amount.amount_1 : null,