开发者

summing API data in flutter an error occurs

I want to make a sum using the api data in the model but when I try it an error occurs as below. did i make a typo or was it not when i want to call sum from API.

summing API data in flutter an error occurs

here is the code i made to do the sum. this is variable initialization. and I want to call the result of the sum here.

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,
                                    ),
                                  ]),
                                ]),
                              ],
                            ),

this is the model of the converted 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;
  }
}

this is when i call data in 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();
    }
  }


Initialize the two variables before using them:

var nilaiIndeksUts = 0;
var nilaiIndeksAkhir = 0;
var result = nilaiIndeksUts + nilaiIndeksAkhir;

Of course you should initialize them to more meaningful values than 0.

UPDATE:

I have given a complete example to this question: How to use the free and public Rapid API and call the API in the flutter Application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜