I tried to do the sum by taking the model that I had made but the results were like. has anyone ever had a similar case and how to solve it.
this is the function that I made to do the addition with the type String parameter because in the API the data type is string.
totalCalculate<NilaiMahasiswa>(nilaiAkhirUas, String nilaiIndeksAkhir) {
int nilaiAkhirUas = int.parse(["nilay_akhir_uas"]!);
double nilaiIndeksAkhir = double.parse(["nilay_akhir"]!);
return nilaiAkhirUas nilaiIndeksAkhir;
}
and this is when I call the result of the sum above or in the function above but an error occurs
Flexible(
child: FutureBuilder<NilaiMahasiswa>(
future: Services().getNilaiMahasiswa(semester),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
// 'IPK ${snapshot.data!.data?.first.updatedBy}',
'IPK $totalCalculate',
style: bold5,
);
} else if (snapshot.hasError) {
print(snapshot.data);
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
),
),
and this is the error message that is displayed
CodePudding user response:
You are trying to parse int from List
int.parse(["nilay_akhir_uas"]!)
. Actually this will raise error. Maybe you just like to read the map, I guess the map is nilaiAkhirUas
. So it is safe to .tryParse
totalCalculate<NilaiMahasiswa>(nilaiAkhirUas, String nilaiIndeksAkhir) {
int nilaiAkhirUas = int.tryParse(nilaiAkhirUas["nilay_akhir_uas"]??"")??0;
double nilaiIndeksAkhir = double.tryParse(nilaiAkhirUas["nilay_akhir"]??"")??0;
return nilaiAkhirUas nilaiIndeksAkhir;
}
Or if those parameters are string, you can do
totalCalculate<NilaiMahasiswa>(nilaiAkhirUas, String nilaiIndeksAkhir) {
int nilaiAkhirUas = int.tryParse(nilaiAkhirUas??"")??0;
double nilaiIndeksAkhir = double.tryParse(nilaiIndeksAkhir)??0;
return nilaiAkhirUas nilaiIndeksAkhir;
}
CodePudding user response:
int.parse
expects String
not List of String
so,
totalCalculate<NilaiMahasiswa>(nilaiAkhirUas, String nilaiIndeksAkhir) {
int nilaiAkhirUas = int.parse(nilaiAkhirUas);
double nilaiIndeksAkhir = double.parse(nilaiIndeksAkhir);
return nilaiAkhirUas nilaiIndeksAkhir;
}
Should work
CodePudding user response:
maybe you wanted to do this:
totalCalculate<NilaiMahasiswa>(nilaiAkhirUas, String nilaiIndeksAkhir) {
int nilay_akhir_uas = int.parse(nilaiAkhirUas);
double nilay_akhir = double.parse(nilaiIndeksAkhir);
return nilay_akhir_uas nilay_akhir;
}