permission to ask, I want to ask how do I do the addition using data in the API, I've been having trouble for days and haven't solved it either I've got the reference but still confused too
this is the json data from the API.
{
"id_transkrip_nilai": "53a6c0c7-9271-4b1a-8b2e-bf942cb0871c",
"id_mk": "b91b6b58-3c22-45e2-8aca-f0fe507d229f",
"kode_mk": null,
"nm_mk": "KPAM-VII (Kapita Selekta)",
"sks": 1,
"smt": 7,
"nilai_akhir_uts": "100",
"nilai_huruf_uts": "A",
"nilai_indeks_uts": "4",
"nilai_akhir_uas": "90",
"nilai_akhir": "93.33",
"nilai_huruf_akhir": "A",
"nilai_indeks_akhir": "4",
"status_nilai_akhir": 3,
"status_nilai_uts": 3,
"updated_by": "Raey"
}
I want to add the value of nilai_indeks_uts nilai_indeks_akhir and I have made the model before.
this is a simple example if the data is already set and comes from locale
example
var firstNumber = 4;
var secondNumber = 13;
var sum = firstNumber secondNumber;
print(sum);
/*
Output :
17
*/
how do i do it if the data is coming from API. please enlighten. Thanks.
class NilaiMahasiswa {
String? status;
String? code;
List<Data>? data;
NilaiMahasiswa({this.status, this.code, this.data});
NilaiMahasiswa.fromJson(Map<String, dynamic> json) {
status = json['status'];
code = json['code'];
if (json['data'] != null) {
data = <Data>[];
json['data'].forEach((v) {
data!.add(Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['status'] = status;
data['code'] = code;
if (this.data != null) {
data['data'] = this.data!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String? idTranskripNilai;
String? idMk;
String? 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;
Data(
{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});
Data.fromJson(Map<String, dynamic> json) {
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'];
nilaiAkhir = json['nilai_akhir'];
nilaiHurufAkhir = json['nilai_huruf_akhir'];
nilaiIndeksAkhir = json['nilai_indeks_akhir'];
statusNilaiAkhir = json['status_nilai_akhir'];
statusNilaiUts = json['status_nilai_uts'];
updatedBy = json['updated_by'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id_transkrip_nilai'] = this.idTranskripNilai;
data['id_mk'] = this.idMk;
data['kode_mk'] = this.kodeMk;
data['nm_mk'] = this.nmMk;
data['sks'] = this.sks;
data['smt'] = this.smt;
data['nilai_akhir_uts'] = this.nilaiAkhirUts;
data['nilai_huruf_uts'] = this.nilaiHurufUts;
data['nilai_indeks_uts'] = this.nilaiIndeksUts;
data['nilai_akhir_uas'] = this.nilaiAkhirUas;
data['nilai_akhir'] = this.nilaiAkhir;
data['nilai_huruf_akhir'] = this.nilaiHurufAkhir;
data['nilai_indeks_akhir'] = this.nilaiIndeksAkhir;
data['status_nilai_akhir'] = this.statusNilaiAkhir;
data['status_nilai_uts'] = this.statusNilaiUts;
data['updated_by'] = this.updatedBy;
return data;
}
}
CodePudding user response:
You may have the following function in Data
class:
int getSum() {
int? uts = int.tryParse(nilaiIndeksUts ?? '');
int? akhir = int.tryParse(nilaiIndeksAkhir ?? '');
return (uts ?? 0) (akhir ?? 0);
}
It would try to parse the String
values of your required variables to int
; if parsed int
values are null
, these will be replaced with 0
, so that it always returns a non null int
.
Finally, u may call this function on any instance of Data
to get the sum.
Hope it helps!
CodePudding user response:
I have modified your model please check the below code.
class NilaiMahasiswa {
String? status;
String? code;
List<Data>? data;
NilaiMahasiswa({this.status, this.code, this.data});
NilaiMahasiswa.fromJson(Map<String, dynamic> json) {
status = json['status'];
code = json['code'];
if (json['data'] != null) {
data = <Data>[];
json['data'].forEach((v) {
data!.add(Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['status'] = status;
data['code'] = code;
if (this.data != null) {
data['data'] = this.data!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String? idTranskripNilai;
String? idMk;
String? kodeMk;
String? nmMk;
int? sks;
int? smt;
String? nilaiAkhirUts;
String? nilaiHurufUts;
int? nilaiIndeksUts; // convert it to int the moment you receive the data
String? nilaiAkhirUas;
String? nilaiAkhir;
String? nilaiHurufAkhir;
int? nilaiIndeksAkhir; // convert it to int the moment you receive thedata
int? statusNilaiAkhir;
int? statusNilaiUts;
String? updatedBy;
int? summationResult;
Data(
{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});
Data.fromJson(Map<String, dynamic> json) {
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'];
// converting to int
nilaiIndeksUts = int.tryParse(json['nilai_indeks_uts']);
nilaiAkhirUas = json['nilai_akhir_uas'];
nilaiAkhir = json['nilai_akhir'];
nilaiHurufAkhir = json['nilai_huruf_akhir'];
// converting to int
nilaiIndeksAkhir = int.tryParse(json['nilai_indeks_akhir']);
statusNilaiAkhir = json['status_nilai_akhir'];
statusNilaiUts = json['status_nilai_uts'];
updatedBy = json['updated_by'];
summationResult = nilaiIndeksAkhir nilaiIndeksUts;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id_transkrip_nilai'] = this.idTranskripNilai;
data['id_mk'] = this.idMk;
data['kode_mk'] = this.kodeMk;
data['nm_mk'] = this.nmMk;
data['sks'] = this.sks;
data['smt'] = this.smt;
data['nilai_akhir_uts'] = this.nilaiAkhirUts;
data['nilai_huruf_uts'] = this.nilaiHurufUts;
data['nilai_indeks_uts'] = this.nilaiIndeksUts;
data['nilai_akhir_uas'] = this.nilaiAkhirUas;
data['nilai_akhir'] = this.nilaiAkhir;
data['nilai_huruf_akhir'] = this.nilaiHurufAkhir;
data['nilai_indeks_akhir'] = this.nilaiIndeksAkhir;
data['status_nilai_akhir'] = this.statusNilaiAkhir;
data['status_nilai_uts'] = this.statusNilaiUts;
data['updated_by'] = this.updatedBy;
return data;
}
}