2
votes

How to check value is exits or not in an array of objects. Please give me a solution if it works then I accept your answer Anyone here with a solution for this? Thanks in Advance. Here is my code.

[{"index":0,"productId":"5d9890c7773be00ab5b41701","qty":2}, {"index":0,"productId":"5d9890c7773be00ab5b41701","qty":3}]

how to check ProductId is exited or not of this array in Flutter.

1

1 Answers

3
votes

Your List have duplicate value. In my demo, add an extra index 1 to prove it work
For unique and index only , use indexWhere
For return qualified list use where , firstWhere, singleWhere
You can see DarPad result
code snippet

void main()  {

 List<Map> productList = [{"index":0,"productId":"5d9890c7773be00ab5b41701","qty":2}, {"index":0,"productId":"5d9890c7773be00ab5b41701","qty":3},                   {"index":1,"productId":"5d9890c7773be00ab5b41702","qty":23}];

  int index = productList.indexWhere((prod) => prod["productId"]=='5d9890c7773be00ab5b41701' ); 
  print(index);

  var prodLists = productList.where((prod) => prod["productId"] == '5d9890c7773be00ab5b41701');

  print('${prodLists}');

}

Output

0
({index: 0, productId: 5d9890c7773be00ab5b41701, qty: 2}, {index: 0, productId: 5d9890c7773be00ab5b41701, qty: 3})

enter image description here