【发布时间】:2021-09-14 21:44:04
【问题描述】:
我有一个 Hive 对象:
import 'package:hive/hive.dart';
part 'cartHiveModel.g.dart';
@HiveType(typeId: 0)
class CartModel extends HiveObject {
// for the product title
@HiveField(0)
late final String title;
// for checking the store id
@HiveField(1)
late final String storeID;
// fot the product image
@HiveField(2)
late final String image;
// for the discounted price
@HiveField(3)
late final int price;
// for the original price
@HiveField(4)
late final int originalPrice;
// for the quantity.
@HiveField(5)
late final int quantity;
CartModel({
required this.image,
required this.originalPrice,
required this.price,
required this.quantity,
required this.storeID,
required this.title,
});
}
当我尝试通过以下方式计算总金额时
var list = Hive.box<CartModel>('cartModel').values;
list.reduce((value, element) => value.price + value.price);
它给了我这样的运行时错误:
The return type 'int' isn't a 'CartModel', as required by the closure's context.
我想要什么:我想计算列表中的总金额并将总和作为整数返回。
【问题讨论】:
标签: flutter dart flutter-hive