flutter - 如何通过 dart 中的另一个项目列表搜索对象列表

标签 flutter dart

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

我能够基于单个字符串进行过滤,但不能基于字符串列表

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));

如果 list 中的项目数很大,您可能需要执行以下操作:
  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));

或者只是始终将匹配的值存储为哈希集。

关于flutter - 如何通过 dart 中的另一个项目列表搜索对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56884062/

相关文章:

ios - Flutter - 代码签名 "App.framework"失败

dart - 为什么颜色没有变化?

layout - 使用 Align 或使用 Positioned flutter 有什么区别

flutter - 使用 Pageview.builder 在 Flutter 中分页

flutter - 使用flutter_bloc在flutter中提交时如何验证表单?

flutter - Flutter函数颜色未定义或未定义类颜色

flutter - 当我使用NotificationListener时,RefreshIndicator在flutter中不起作用

css - 如何在 Dart/Polymer 中隐藏卡片上的元素标签?

dart - Pub 失败,[1] 解决依赖关系... code_transformers 的版本约束不兼容

debugging - Dart WebGL 调试