问题描述
我正在使用 Flutter 制作一个应用程序并使用 hive 创建一个数据库,用户将获得一个选项以根据某些字段保存交易详细信息 现在我想要的是,根据交易日期,特定月份所有交易的总和,然后在这样的特定页面上显示其总和
我用来创建表单域的代码
class Transaction extends HiveObject {
@HiveField(0)
String paymentMode;
@HiveField(1)
bool unnecessary;
@HiveField(2)
String title;
@HiveField(3)
DateTime createdOn;
@HiveField(4)
int amount;
@HiveField(5)
String category;
Transaction({
this.paymentMode = "",this.unnecessary = false,this.title = "",this.createdOn,this.amount = 0,this.category="",});
Transaction.fromJson(Map<String,dynamic> map) :
paymentMode = map['paymentMode'] ?? "",unnecessary = map['unnecessary'] ?? false,title = map['title'] ?? "",createdOn = map['createdOn'] ?? DateTime.Now(),amount = map['amount'] ?? 0,category = map['category'] ?? "";
Map<String,dynamic> toJson() => {
'paymentMode': paymentMode,'unnecessary': false,'title': title,'createdOn': createdOn,'amount': amount,'category': category,};
Transaction copyWith({
String paymentMode,bool unnecessary,String title,DateTime createdOn,User user,int amount,String category,}) {
return Transaction(
paymentMode: paymentMode ?? this.paymentMode,unnecessary: unnecessary ?? this.unnecessary,title: title ?? this.title,createdOn: createdOn ?? this.createdOn,amount: amount ?? this.amount,category: category ?? this.category,);
}
解决方法
我假设您有一个 Transaction
对象列表,
您可以简单地使用排序功能对该列表进行排序。
listOftransactions.sort((a,b)=>a.createdOn.compareTo(b.createdOn));
这将根据时间戳对列表进行排序,如果您想颠倒顺序,只需将内联函数中的 a
替换为 b
。