rangeError (RangeError (index): Invalid value: Not in inclusive range 0..4: 5)

Viewed 28

So basically I'm making an Inventory Management System using flutter desktop. this is my code,

InventoryPage.dart

    // get all posts
  Future<void> getData() async {
    Response response = await getInventoryMonthly(tanggal);

    if (response.error == null) {
      setState(() {
        _inventoryList = response.data as List;
        if(formattedLastDate == DateFormat('yyyy-MM-dd').format(DateTime(date.year, date.month+1, 0))){
          print("Benar!");
              }else{
            for (var i = 0; i < _inventoryList.length; i++) {
                addInventory(
                  _inventoryList[i]['id_kategori'], 
                  _inventoryList[i]['id_gudang'], 
                  _inventoryList[i]['id_brand'], 
                  _inventoryList[i]['nama'], 
                  _inventoryList[i]['parts_no'], 
                  _inventoryList[i]['new_parts'], 
                  _inventoryList[i]['satuan'], 
                  _inventoryList[i]['jumlah_akhir'], 
                  0.0,
                  0.0,
                  _inventoryList[i]['jumlah_akhir'], 
                  _inventoryList[i]['harga'], 
                  _inventoryList[i]['image'], 
                  tanggal,
                  ); 
                  }
                  print(formattedLastDate);
                  print(DateFormat('yyyy-MM-dd').format(DateTime(date.year, date.month+1, 0)));
                  // print(formattedLastDate);
                print("salah!");
              }

        _loading = _loading ? !_loading : _loading;
      });
    } else if (response.error == unauthorized) {
      logOut().then((value) => {
            Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(builder: (context) => Login()),
                (route) => false)
          });
    } else {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text('${response.error}'),
      ));
    }
  }

AddInventory Future

// Add barang
Future<Response> addInventory(
  int? id_kategori,
  int? id_gudang,
  int? id_brand,
  String? nama,
  var parts_no,
  var new_parts,
  var satuan,
  num? jumlah_awal,
  num? jumlah_masuk,
  num? jumlah_keluar,
  num? jumlah_akhir,
  num? harga,
  var image,
  var created_month,
) async {
  Response res = Response();
  try {
    String token = await getToken();
    final response = await http.post(Uri.parse(barangURL),
        headers: {
          'Accept': 'application/json',
          'Authorization': 'Bearer $token'
        },
        body: image != null
            ? {
                "id_kategori": id_kategori.toString(),
                "id_gudang": id_gudang.toString(),
                "id_brand": id_brand.toString(),
                "nama": nama,
                "parts_no": parts_no,
                "new_parts": new_parts,
                "satuan": satuan,
                "jumlah_awal": jumlah_awal.toString(),
                "jumlah_masuk": jumlah_masuk.toString(),
                "jumlah_keluar": jumlah_keluar.toString(),
                "jumlah_akhir": jumlah_akhir.toString(),
                "harga": harga.toString(),
                "image": image,
                "created_month": created_month,
              }
            : {
                "id_kategori": id_kategori.toString(),
                "id_gudang": id_gudang.toString(),
                "id_brand": id_brand.toString(),
                "nama": nama,
                "parts_no": parts_no,
                "new_parts": new_parts,
                "satuan": satuan,
                "jumlah_awal": jumlah_awal.toString(),
                "jumlah_masuk": jumlah_masuk.toString(),
                "jumlah_keluar": jumlah_keluar.toString(),
                "jumlah_akhir": jumlah_akhir.toString(),
                "harga": harga.toString(),
                "created_month": created_month.toString(),
              });
    switch (response.statusCode) {
      case 200:
        print(jsonDecode(response.body));
        res.data = jsonDecode(response.body);
        break;
      case 422:
        final errors = jsonDecode(response.body)['errors'];
        res.error = errors[errors.keys.elementAt(0)][0];
        break;
      case 401:
        res.error = unauthorized;
        break;
      default:
        res.error = somethingWentWrong;
        break;
    }
  } catch (e) {
    res.error = serverError;
  }

  return res;
}

 

My goal is to simply make it so that flutter will automatically create Similiar Data and values at the end of the month. This is one of the error that I've got: rangeError (RangeError (index): Invalid value: Not in inclusive range 0..4: 5)

Please can anyone help me with this? Thanks in advance

1 Answers

There might be issue because you initialised int i = 0 separately. You can re-write code of line no.152 as shown below

for (int i = 0; i < storage.lenght; i++){
   response = await addInventory(){
     //Further code
   }
}
Related