【问题标题】:Flutter: How to search a 'set' for an object based on the object's field valueFlutter:如何根据对象的字段值搜索对象的“集合”
【发布时间】:2019-11-30 03:02:32
【问题描述】:

根据标题,我如何通过对象字段值搜索对象的“集合”。以下是不正确的代码,但希望能给出想法。我能找到的最接近的是How to search for an object in List<object> having its field value,但似乎并不适用。

class MyClass {
  final String a;
  final int b;
  final double c;
  MyClass({this.a, this.b, this.c});
}

class Check{
   Set<MyClass> mSet = new Set<MyClass>();
   contains() {
     mSet.add(new MyClass(a: "someString1", b:3, c:2.0));
     mSet.add(new MyClass(a: "someString2", b:1, c:3.0));
     mSet.add(new MyClass(a: "someString3", b:2, c:1.0));
     //following is not correct, but to get the idea...
     print(mSet.contains((item) => item.a == "someString1"));
     //need to return true
   }
}

>FALSE

同样的问题可能适用于 Set.where、Set.remove ...等,例如,如果我添加

var b = mSet.where((item) => item.a == "someString1");

b 产生一个具有 所有 Set 值的迭代。

【问题讨论】:

    标签: flutter dart set contains


    【解决方案1】:

    总结一下 Kris 的 cmets,要从对象字段的查询中返回布尔值,不能使用 'contains'。相反,您可以使用 whereSet 类中采用测试元素的其他方法,例如 (bool test(E element), { E orElse() }) → E) (https://api.dartlang.org/stable/2.6.1/dart-core/Set-class.html), 并添加一个三元运算符以返回所需的布尔结果。

    void main() {
      print(Check().contains());
    }
    
    class MyClass {
      final String a;
      final int b;
      final double c;
      MyClass({this.a, this.b, this.c});
    }
    
    class Check{
       Set<MyClass> mSet = new Set<MyClass>();
       bool contains() {
         mSet.add(new MyClass(a: "someString1", b:3, c:2.0));
         mSet.add(new MyClass(a: "someString2", b:1, c:3.0));
         mSet.add(new MyClass(a: "someString3", b:2, c:1.0));
         return mSet.where((item) => item.a  == "someString1").length > 0;
       }
    }
    
    >>true
    

    EDIT:在尝试了一些其他“测试元素”方法(例如 firstWherelastWhere) 似乎 'where' 是我测试过的唯一在没有匹配项时不会失败的方法。如果你使用其他你需要一个 catch 然后返回 false。根据上面修改后的代码,我坚持使用 'where' 和三元组以获得最干净的解决方案。如果只有 'contains' 包含一个测试元素。

    【讨论】:

      【解决方案2】:

      在 dart 中,Set 扩展了 Iterable。 There are lots of useful methods that you can call on an iterable that effectively sort the iterable according to some value

      例如,你可以说:

      void main() {
      
        Check().contains();
      
      }
      
      
      class MyClass {
        final String a;
        final int b;
        final double c;
        MyClass({this.a, this.b, this.c});
      
        @override
        String toString(){
          return a;
        }
      }
      
      class Check{
         Set<MyClass> mSet = new Set<MyClass>();
         contains() {
           mSet.add(new MyClass(a: "someString1", b:3, c:2.0));
           mSet.add(new MyClass(a: "someString2", b:1, c:3.0));
           mSet.add(new MyClass(a: "someString3", b:2, c:1.0));
      
           print(mSet.firstWhere((item) => item.a == "someString1"));
      
         }
      }
      

      【讨论】:

      • 感谢 Kris,实际上只是“包含”不包括条件选项。我可能会删除这篇文章,因为它代表我的一个简单的忽略。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 2016-10-31
      • 1970-01-01
      • 2020-11-07
      • 2021-04-12
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多