How to search a list of Object by another list of items in dart

后端 未结 5 1343
一向
一向 2021-02-14 08:00

How to search a list of a class object with one of its property matching to any value in another list of strings

I am able to get filtering based on a single string , bu

5条回答
  •  礼貌的吻别
    2021-02-14 08:13

      List list = list to search;
      List matchingList = list of strings that you want to match against;
    
      list.where((item) => matchingList.contains(item.relevantProperty));
    

    If the number of items in list is large, you might want to do:

      List list = list to search;
      List matchingList = list of strings that you want to match against;
    
      final matchingSet = HashSet.from(matchingList);
    
      list.where((item) => matchingSet.contains(item.relevantProperty));
    

    Or else just always store the matching values as a hashset.

提交回复
热议问题