【问题标题】:Filter list with whereIn condition in Flutter?Flutter中带有whereIn条件的过滤器列表?
【发布时间】:2021-07-25 10:49:29
【问题描述】:

此代码示例运行良好。

var box = await Hive.openBox<MainWords>('mainWords');
box.values.where((item)  {
      return item.category == "6" || item.category == '13';
    }).toList();

我正在尝试使用 whereIn 条件过滤列表,但它必须像过滤器一样过滤

List<String> categoryList = ['6', '13'];
var box = await Hive.openBox<MainWords>('mainWords');
box.values.where((item)  {
      return item in categoryList; // just an examle
    }).toList();

我怎样才能做到这一点?

【问题讨论】:

  • 可能是categoryList.contains(item)?,您是否尝试根据categoryList 过滤值?请说明您要做什么

标签: flutter filter flutter-hive


【解决方案1】:

您不应该使用关键字in,而是使用方法contains 来检查您的item 是否存在于categoryList 中。此外,您无法比较不同类型的值,我看到您返回的是 box.valuesIterable&lt;MainWords&gt;

我不知道这个类的内容,但是item 变量是MainWords 类型,所以不能直接与String 对象比较。

我假设您可以访问您的班级 MainWords 中的 String 值,因此您需要将此值与您的列表进行比较。

代码示例

List<String> categoryList = ['6', '13'];
var box = await Hive.openBox<MainWords>('mainWords');

// As I don't know what are MainWords' properties I named it stringValue.
box.values.where((item) => categoryList.contains(item.stringValue)).toList();

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多