Home > Mobile >  How can i add item to list but not the ones are already added
How can i add item to list but not the ones are already added

Time:03-15

I am trying to add unique items to a list so that I avoid repetition of them.

List listOne = ['a','b','c','d'];
List listTwo = ['a','b'];
    
listTwo.add(/* Here i need to add : ['e','f','a','b']*/) // but avoid 'a','b' because they already exist. 

I have done it by making a loop like following

for(final test in listOne ) {
  if(!listTwo.contains(test)) {
    listTwo.add(test);
  }
}

But I don't think it is a good way for big data.

Is there any good way for handling this?

CodePudding user response:

List listOne = ['a','b','c','d'];
List listTwo = ['a','b'];
Set listTwoSet = new  Set.from(listTwo);
listTwoSet.addAll(listOne);

CodePudding user response:

listOne = ['a','b','c','d'];
listTwo = ['a','b'];
mergedArray = new Set(listOne .concat(listTwo ));
  •  Tags:  
  • dart
  • Related