skip to Main Content

I have a listview as drawn and I want to add up the values that I have marked in red, how do I do that to be able to add the values above.

enter image description here

and this is the model used in the listview above, and the value you want to add up is the ‘sks’ value in the model with the INT data type

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,
    };
}

there are some references that I saw but most of them use a list that already has a value for example as below, but what if the data comes from the 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

Answers


  1. import build in collection package :

    import 'package:collection/collection.dart';
    

    map your data from api to new list

    final listMutu = data.map((e) => int.tryParse(e.nilaiIndeksAkhir ?? '') ?? 0).toList();
    

    use .sum method to get the sum of collection:

    return ListTile(
      title: const Text('Total Mutu'),
      subtitle: Text('${listMutu.sum}'),
    );
    
    Login or Signup to reply.
  2. you can add another fucntion to your SumValue class

    class SumValue {
     ....
     int get sumCountData => data.fold(0,(int prev, data)=>prev+ data.sks);
    

    you can see full demo here
    https://dartpad.dev/?id=84fc22275a395a86ccc8783ffa319baa

    • example how to use it:
    void main() {
      final exampleJson = {
        'category': 'lorem ipusm',
        'data': [
          {'count': 10, 'name': 'abc'},
          {'count': 10, 'name': 'ab'},
          {'count': 10, 'name': 'a'},
        ]
      };
    AnotherData anoterData = AnotherData.fromJson(exampleJson);
      
    print(anoterData.sumCountData); // 30
      
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search