我想使用模型中的API数据进行求和,但当我尝试时,出现了如下错误。我是否犯了错字,或者当我想从API调用求和时,没有出现错字。
这是我做的求和的代码。这是变量初始化。我想在这里调用求和的结果。
Expanded(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, index) {
var nilaiIndeksUts = 0;
var nilaiIndeksAkhir = 1;
var result = nilaiIndeksUts + nilaiIndeksAkhir;
return Padding(
padding: const EdgeInsets.only(
bottom: 6,
top: 6,
),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(
1,
2,
),
),
],
),
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
data[index].nmMk ?? '',
style: bold5,
),
const Divider(),
Table(
defaultColumnWidth: const FlexColumnWidth(),
border: const TableBorder(
verticalInside: BorderSide.none,
),
children: [
TableRow(children: [
Column(
children: [
Text(
'Kode MK',
style: regular7,
)
],
),
Column(
children: [
Text(
'Huruf Mutu',
style: regular7,
)
],
),
Column(
children: [
Text(
'Angka Mutu',
style: regular7,
)
],
),
Column(
children: [
Text(
'Mutu SKS',
style: regular7,
)
],
),
Column(
children: [
Text(
'Nilai Mutu',
style: regular7,
)
],
),
]),
TableRow(children: [
Column(children: [
Text(
'${data[index].kodeMk}',
style: bold6,
)
]),
Column(children: [
Text(
'${data[index].nilaiHurufUts}',
style: bold6,
)
]),
Column(children: [
Text(
'${data[index].nilaiIndeksAkhir}',
style: bold6,
)
]),
Column(children: [
Text(
'${data[index].sks}',
style: bold6,
),
]),
Column(children: [
Text(
'$result',
// '${data[index].nilaiIndeksAkhir} ',
style: bold6,
),
]),
]),
],
),
这是转换后的API的模型
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;
}
}
这是我在API中调用数据的时候
Future<NilaiMahasiswa> getNilaiMahasiswa(int semester) async {
String url = Constant.baseURL;
String token = await UtilSharedPreferences.getToken();
final response = await http.get(
Uri.parse(
'$url/auth/mhs_siakad/transkrip_nilai?semester=$semester',
),
headers: {
'Authorization': 'Bearer $token',
},
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 200) {
return NilaiMahasiswa.fromJson(jsonDecode(response.body));
} else {
throw Exception();
}
}
1条答案
按热度按时间krcsximq1#
在使用这两个变量之前,请对其进行初始化:
当然,您应该将它们初始化为比0更有意义的值。
更新:
对于这个问题,我举了一个完整的例子:如何使用免费公开的RapidAPI并在flutter应用程序中调用该API