Home > Enterprise >  how to delete null elements from a object list in flutter
how to delete null elements from a object list in flutter

Time:02-18

I have such a list:

[{id: null, secilenMiktar: null, depoId: null}, {id: 3737, secilenMiktar: 1.0, depoId: 13}, {id: null, secilenMiktar: null, depoId: null}, {id: null, secilenMiktar: null, depoId: null}] 

I want to delete all elements with null id from this list

like this: [{id: 3737, secilenMiktar: 1.0, depoId: 13}]

Thank you!

edit: (Regarding pskink's answer) enter image description here

CodePudding user response:

So let's say you have this list here.

 final List<Map<String, dynamic>> myList = [
    {"id": null, "secilenMiktar": null, "depoId": null},
    {"id": 3737, "secilenMiktar": 1.0, "depoId": 13},
    {"id": null, "secilenMiktar": null, "depoId": null},
    {"id": null, "secilenMiktar": null, "depoId": null},
  ];

To remove elements where the id is null, you can use the removeWhere and check if the the current Map with the key id is null.

 myList.removeWhere((e) => e["id"] == null);

CodePudding user response:

this worked


 (requestObj["detayIds"] as List<Map<String, dynamic>>)
          .removeWhere((element) => element.values.first == null);

  • Related