Sort Nested List In Flutter

I have a nested Json of products that I would like to sort by their price that is nested within the json structure. Right now when I try to sort via price, I get an error and I don't understand the error:

Class 'List<Product>' has no instance getter 'sizes'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: sizes

If I'm trying to sort the nested filter, shouldn't I call sizes? I cannot get cases 1 or 2 working.

I'm trying filter my like this:

  void _sortProductsDropDown(_fabSort) {
    var tempProducts;

    setState(() {
      switch (_fabSort) {
        case 0:
tempProducts = [];
          break;
        case 1:
          tempProducts =
              filteredProducts.sizes.sort((a, b) => a.price.compareTo(b.price));

          print('Attempt $tempProducts');

          break;
        case 2:
          tempProducts = filteredProducts
              .Sodium.sort;
          print('Sodium sort + ${tempProducts.length}');

          break;
        case 3:
          tempProducts =
              filteredProducts.where((e) => e.caloriesContent == 0.0).toList();
          print(‘no cal  ${tempProducts}');

          break;
      }
    });
  }

My json data

            "id": 49,
            "name": “Celery Combo”,
            "short_description": “Yummy no calories”,

            "quantity": 26,
            "sizes": [
                {
                    "id": 47,
                    "size": 6,
                    "product_id": 49,
                    "price": 8.99
                }
            ],
            "image": "https://ges3334.amazonaws.com/product_images/
            “sodium,”: 7.0
        },
        {
            "id": 48,
            "name": “Beef Burger“,
            "short_description": “Mad Cow Calories”,

            "quantity": 30,
            "sizes": [
                {
                    "id": 46,
                    "size": 6,
                    "product_id": 48,
                    "price": 8.99
                }
            ],
            "image": "https://ges3334.amazonaws.com/product_images/single_cRIoNU8.png",


            “calorie”: 1200.0,
            “sodium”: 26.0
        }
    ]
}

Upvotes: 0

Views: 840

Answers (1)

Lesiak
Lesiak

Reputation: 25936

List does not have sizes property, sizes is property of each product. This is generated by this line

filteredProducts.sizes.sort((a, b) => a.price.compareTo(b.price));

and clearly reported:

Class 'List<Product>' has no instance getter 'sizes'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: sizes

In your comment, you say that you want to sort products by price, where price of Product is found by looking at first item in sizes list.

It is easy to achieve (here I used helper function, but a lambda is also ok):

int compareProductBySmallestSize(Product lhs, Product rhs) {
  var lhsPrice = lhs.sizes[0].price;
  var rhsPrice = rhs.sizes[0].price;
  return lhsPrice.compareTo(rhsPrice);
}

void _sortProductsDropDown(_fabSort) {
   // ...
   products.sort(compareProductBySmallestSize);
   // ...
}

Note that if products with empty sizes list are allowed, a small amendment must be made in the helper function.

Upvotes: 1

Related Questions