【问题标题】:How to search through the Object list with the property in dart如何使用 dart 中的属性搜索对象列表
【发布时间】:2020-09-03 14:27:40
【问题描述】:

我有这样的列表:-

[obj1, obj2, obj3, obj4, obj5]

在 dart 中找到上面列表中 obj.id == "id" 的对象的最优雅方法是什么?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    一种方法是使用.where 方法。

    例如

    class Foo{
     final String id;
     Foo(this.id);
    }
    
    main(){
     List<Foo> foos = List.generate(5, (index) => Foo("${index+1}")); //Creates Foo objects with ID from 1 to 5;
     String idToSearch = "3";
     Foo foundFoo = foos.where((foo) => foo.id == idToSearch));
     print(foundFoo.id); //3
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多