如何通过dart中的另一个项目列表搜索对象列表 [英] How to search a list of Object by another list of items in dart

查看:98
本文介绍了如何通过dart中的另一个项目列表搜索对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何搜索其属性之一与另一个字符串列表中的任何值匹配的类对象列表

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 , but not on a list of strings

final List<shop_cart.ShoppingCart> cartprd = snapshot.documents
      .map((f) => shop_cart.ShoppingCart.fromMap(f.data))
      .toList();


推荐答案

  List<SomeClass> list = list to search;
  List<String> 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<SomeClass> list = list to search;
  List<String> 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.

这篇关于如何通过dart中的另一个项目列表搜索对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆