Home > Blockchain >  search for an item into list<map>
search for an item into list<map>

Time:08-25

Good Morning everyone, I want ask a question if I have a list of map like this

[{
   id: 1, 
   title: Medicine 500, 
   price: 100, 
   image: http://secret-taiga-11502.herokuapp.com/images/medicines, 
   quantity: 1, 
   pharmacyid: 15, 
   medicineID: 500
}]

and I want to search for an item medicineID to make a compression

how can I do this

this my code

var extractMap = cubit.myItems.map(
  (element) => Map.fromEntries(
  [MapEntry('medicine_id', element['medicineID']),])).toList();

if(
extractMap.contains(
cubit.onePharmacyModel!.data!.activeMedicines![index].id!.toInt())
){
cubit.updateQuery(
    cubit.myItems[index]["quantity"], 
  cubit.myItems[index]['id'], 
  cubit.onePharmacyModel!.data!.id!.toInt()
);
Fluttertoast.showToast(msg: "cart updated");

}else{
cubit.insertToDatabase(
  title:       cubit.onePharmacyModel!.data!.activeMedicines[index].name.toString(),
  
  price: cubit.onePharmacyModel!.data!.activeMedicines[index].pivot!.price!.toString(),

  image: cubit.onePharmacyModel!.data!.activeMedicines[index].photo.toString(),
  quantity: 1,

  pharmacyID: cubit.onePharmacyModel!.data!.id!.toInt(),

  medicineID: cubit.onePharmacyModel!.data!.activeMedicines[index].id!.toInt());
                                            Fluttertoast.showToast(msg: "product added to cart");

}

the general idea of code that the user when add a product to the cart I'm checking if it's already exist or not if yes => I'll update the amount of this product, if no => T'll insert it into my Database

CodePudding user response:

Try something like:

if (myItems.any((e) => e['medicineID'] == 58)) {
  // ...
}
  • Related