我有一个列表视图,我想把我用红色标记的值加起来,我该怎么做才能把上面的值加起来。
这是上面列表视图中使用的模型,您要添加的值是INT数据类型模型中的“sks”值
class SumValue {
SumValue({
this.status,
this.code,
this.data,
});
String status;
String code;
List<Datum> data;
factory SumValue.fromJson(Map<String, dynamic> json) => SumValue(
status: json["status"],
code: json["code"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"code": code,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.idTranskripNilai,
this.idMk,
this.kodeMk,
this.nmMk,
this.sks,
this.smt,
this.nilaiAkhirUts,
this.nilaiHurufUts,
this.nilaiIndeksUts,
this.nilaiAkhirUas,
this.nilaiAkhir,
this.nilaiHurufAkhir,
this.nilaiIndeksAkhir,
this.statusNilaiAkhir,
this.statusNilaiUts,
this.updatedBy,
});
String idTranskripNilai;
String idMk;
dynamic kodeMk;
String nmMk;
int sks;
int smt;
String nilaiAkhirUts;
String nilaiHurufUts;
String nilaiIndeksUts;
String nilaiAkhirUas;
String nilaiAkhir;
String nilaiHurufAkhir;
String nilaiIndeksAkhir;
int statusNilaiAkhir;
int statusNilaiUts;
String updatedBy;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
idTranskripNilai: json["id_transkrip_nilai"],
idMk: json["id_mk"],
kodeMk: json["kode_mk"],
nmMk: json["nm_mk"],
sks: json["sks"],
smt: json["smt"],
nilaiAkhirUts: json["nilai_akhir_uts"],
nilaiHurufUts: json["nilai_huruf_uts"],
nilaiIndeksUts: json["nilai_indeks_uts"],
nilaiAkhirUas: json["nilai_akhir_uas"] == null ? null : json["nilai_akhir_uas"],
nilaiAkhir: json["nilai_akhir"] == null ? null : json["nilai_akhir"],
nilaiHurufAkhir: json["nilai_huruf_akhir"] == null ? null : json["nilai_huruf_akhir"],
nilaiIndeksAkhir: json["nilai_indeks_akhir"] == null ? null : json["nilai_indeks_akhir"],
statusNilaiAkhir: json["status_nilai_akhir"] == null ? null : json["status_nilai_akhir"],
statusNilaiUts: json["status_nilai_uts"] == null ? null : json["status_nilai_uts"],
updatedBy: json["updated_by"],
);
Map<String, dynamic> toJson() => {
"id_transkrip_nilai": idTranskripNilai,
"id_mk": idMk,
"kode_mk": kodeMk,
"nm_mk": nmMk,
"sks": sks,
"smt": smt,
"nilai_akhir_uts": nilaiAkhirUts,
"nilai_huruf_uts": nilaiHurufUts,
"nilai_indeks_uts": nilaiIndeksUts,
"nilai_akhir_uas": nilaiAkhirUas == null ? null : nilaiAkhirUas,
"nilai_akhir": nilaiAkhir == null ? null : nilaiAkhir,
"nilai_huruf_akhir": nilaiHurufAkhir == null ? null : nilaiHurufAkhir,
"nilai_indeks_akhir": nilaiIndeksAkhir == null ? null : nilaiIndeksAkhir,
"status_nilai_akhir": statusNilaiAkhir == null ? null : statusNilaiAkhir,
"status_nilai_uts": statusNilaiUts == null ? null : statusNilaiUts,
"updated_by": updatedBy,
};
}
我看到了一些参考文献,但大多数都使用了一个已经有值的列表,例如下面的例子,但如果数据来自API呢
void main() {
List<Data> listData = [
Data(count: 10, name: 'a'),
Data(count: 12, name: 'bc'),
Data(count: 21, name: 'abc'),
];
int sum = listData.fold(0, (int preValue, data) => preValue + data.count);
print(sum);// 43
}
class Data {
int count;
String name;
Data({required this.count, required this.name});
}
2条答案
按热度按时间yvgpqqbh1#
导入收集包中生成:
Map你的数据从API到新列表
使用
.sum
方法得到集合之和:7qhs6swi2#
你可以添加另一个函数到你的
SumValue
类您可以在这里看到完整的演示https://dartpad.dev/?id=84fc22275a395a86ccc8783ffa319baa