how can I find a list contains in any element another list in flutter?

4,895

Solution 1

This should help you...

var firstList = [2, 2, 2, 3];
var secondList = [3, 3, 3];

var firstListSet = firstList.toSet();
var secondListSet = secondList.toSet();

print(firstListSet.intersection(secondListSet));

Solution 2

there is plenty of ways to do it, you could use every() and contains() methods to achieve this

this is how I would do it:

  if (secondList.every((item) => firstList.contains(item))) {
    return true;
  } else {
    return false;
  }

Solution 3

And one more quick way just to check true or false

var firstList = [2, 2, 2, 3];
var secondList = [3, 3, 3];
check(int value) => firstList.contains(value);
bool res = secondList.any(check); // returns true
Share:
4,895
katre
Author by

katre

Updated on December 17, 2022

Comments

  • katre
    katre over 1 year
    var firstList= [1,2,3,4,5];
    var secondList= [3,5];
    
    // compare result : 3,5
    // return true
    
    var firstList= [1,2,3,4,5];
    var secondList= [6,7,8];
    
    // compare result : null
    // return false
    

    How can I compare elements the two lists? If there is matching data in the two lists, return true. if there is no match, return false