Sorting 在flatter中对嵌套列表排序
我有一个嵌套的Json产品,我想按照嵌套在Json结构中的价格对其进行排序。现在,当我尝试通过price进行排序时,我得到一个错误,我不理解错误:Sorting 在flatter中对嵌套列表排序,sorting,flutter,dart,Sorting,Flutter,Dart,我有一个嵌套的Json产品,我想按照嵌套在Json结构中的价格对其进行排序。现在,当我尝试通过price进行排序时,我得到一个错误,我不理解错误: Class 'List<Product>' has no instance getter 'sizes'. Receiver: Instance(length:5) of '_GrowableList' Tried calling: sizes 我的json数据 "id": 49, "na
Class 'List<Product>' has no instance getter 'sizes'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: sizes
我的json数据
"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
}
]
}
列表没有sizes属性,sizes是每个产品的属性。这是由这一行生成的
filteredProducts.sizes.sort((a, b) => a.price.compareTo(b.price));
并明确报告:
Class 'List<Product>' has no instance getter 'sizes'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: sizes
请注意,如果允许产品具有空的
大小
列表,则必须在helper函数中进行小的修改。错误很明显:列表
没有大小
属性,大小
是每个产品的属性。不清楚您正在尝试完成什么:对每个产品中的尺寸
列表进行排序,或对尺寸
中的产品按价格进行排序(但如果一个产品中有多个尺寸怎么办?)按尺寸中的第一项进行排序,以按最低价格显示所有产品。通过这种方式,我可以更新列表生成器中的数据。您将如何传递函数的结果?哪个函数?我是否访问产品中的数据。排序(compareProductBySmallestSize)?我为不清楚而道歉。它返回一个空值。如果数据有效,则.List.sort()不会返回任何内容,而是对列表本身进行排序。看见它返回void,而不是null。恐怕我还是不明白你的问题
Class 'List<Product>' has no instance getter 'sizes'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: sizes
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);
// ...
}