如何对嵌套的ObjectData Flutter进行排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何对嵌套的ObjectData Flutter进行排序相关的知识,希望对你有一定的参考价值。
我正在尝试对具有嵌套数据的JSON对象进行排序。我想在每次按下按钮时刷新对象列表,但是这样做有问题。我正在尝试按价格排序,这是到目前为止的代码:
数据:
{
"id": 152,
"name": "Advil",
"short_description": "Advil 20 Pack",
"usage": "Headaches",
"effect": "MoodSwings",
"sizes": [
{
"id": 307,
"size": 4,
"product_id": 152,
"price": 4.99
},
{
"id": 308,
"size": 5,
"product_id": 152,
"price": 12.66
}
],
}
我的模型设置如下:
Product(
{this.id,
this.name,
this.shortDescription,
this.image,
this.image2,
this.image3,
this.type,
this.sizes,
this.usage,
this.filter,
this.category,
this.prodSize});
factory Product.fromJSON(Map<String, dynamic> json) {
var sizes = json['sizes'] as List;
print(sizes.runtimeType);
List<ProductSizes> sizeList =
sizes.map((i) => ProductSizes.fromJSON(i)).toList();
return Product(
id: json['id'],
name: json['name'],
shortDescription: json['short_description'],
image: json['image'],
image2: json['image2'],
image3: json['image3'],
type: json['Product_Type'],
sizes: sizeList,
usage: json['usage'],
filter: json['filter'],
category: json['Product_Category'],
);
}
}
class ProductSizes {
int id;
int size;
int productId;
double price;
ProductSizes({this.id, this.size, this.productId, this.price});
factory ProductSizes.fromJSON(Map<String, dynamic> json) {
return ProductSizes(
id: json['id'],
size: json['size'],
productId: json['product_id'],
price: json['price']);
}
}
与下拉按钮绑定的我的开关盒:
void _sortProductsDropDown(_filterDropdown, productList) {
var tempProducts;
setState(() {
switch (_filterDropdown) {
case 'Price: Low-High':
for (int i = 0; i < productList.length; i++) {
tempProducts.add(productList[i]
.sizes
.sort((a, b) => b.$price.reversed(a.price)));
}
_productTabFilter = tempProducts;
print(tempProducts.toString());
break;
case 'Price: High-Low':
print(_filterDropdown);
for (int i = 0; i < productList.length; i++) {
tempProducts.add(productList[i]
.sizes
.sort((a, b) => a.$price.compareTo(b.price)));
}
_productTabFilter = tempProducts;
print(tempProducts.toString());
break;
}
});
// return productList;
}
这是通过此处的下拉按钮的onchange方法调用的:
void _changeFilterList(String value) {
//use _filterDropdown for switch statement
setState(() {
_filterDropdown = value;
});
print(' the value of the dropdown is $_filterDropdown');
_sortProductsDropDown(_filterDropdown, productList);
}
然后我使用gridview构建器制作数据
return GridView.builder(
itemCount: _productTabFilter.length,
controller: ScrollController(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 20,
childAspectRatio: 0.7),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemBuilder: (context, i) {
然后引发此错误:
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: type '(dynamic, dynamic) => dynamic' is not a subtype of type '(ProductSizes, ProductSizes) => int' of 'compare'
任何帮助将不胜感激
答案
尝试将该对象转换为列表,然后在该列表上使用排序功能。
像这样:
(productList[i]['sizes'] as List).sort((a, b) => a['price'].compareTo(b['price']))
以上是关于如何对嵌套的ObjectData Flutter进行排序的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter 中嵌套 StreamBuilder?