类列表 Flutter 中另一个成员的访问成员

时间:2021-07-05 06:58:32

标签: flutter dart

我正在尝试使用下面示例中 modelList 中的 name 查找 id

class Example{
  List<Abc> modelList = [
    Abc(1, "John"),
    Abc(2, "Christine"),
    Abc(3, "Steven"),
    Abc(4, "Others"),
  ];
  
  myFun(){
    int idToFind = 4;
    String foundString = // Some iterable function??
  }
}

class Abc{
  int id;
  String name;
  Abc(this.id, this.name);
}

1 个答案:

答案 0 :(得分:1)

String foundString = modelList.firstWhere((abc) => abc.id == idToFind).name;
相关问题