Search code examples
flutterdartdart-null-safety

I have checked for null value but still get error saying I didn't checked


enter image description here I already have a null check before using the value but it still give me errorenter image description here


Solution

  • You can put an ! behind it to indicate you are sure it can't be null, so like

    for (final value in widget.details.data.productPriceObjs!) {
    

    If you don't like using ! you can alternatively write

    var objects = widget.details.data.productPriceObjs;
    if (objects != null) {
      for (final value in objects) {
      
      }
    }
    

    Somehow the compiler is fine with it when there isn't "nesting" in the check