skip to Main Content

I want to check if items in array existed and add new values from another array without overwriting element after reloading. I created such code:

//take from that array
    List<int> list = [2, 3, 5];
  // add to this array and check if this array already has the same element or not    
      List<int> newValueInt = [2, 6, 7];
    
      list.forEach((item) {
        if(!list.contains(item)){
          newValueInt.add(item);
          print(newValueInt);
        }  
      });

and it shows me that print:

     [2, 6, 7, 3]
[2, 6, 7, 3, 5]

2

Answers


  1. if(!list.contains(item)){
    

    to

    if(! newValueInt.contains(item)){
    
    Login or Signup to reply.
  2. List<int> list = [2, 3, 5];
      // add to this array and check if this array already has the same element or not
      List<int> newValueInt = [2, 6, 7];
      List<int> temp = [];
      for (var item in list) {
        if(!newValueInt.contains(item)){
          temp.add(item);
        }
      }
    
      List<int> result = newValueInt + temp;
    
      print(result); //---> [2, 6, 7, 3, 5]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search