VipiN Negi
VipiN Negi

Reputation: 3108

Access member by another member in a Class List Flutter

I'm trying to find the name using the id in modelList from the example below.

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

Upvotes: 0

Views: 131

Answers (1)

eyoeldefare
eyoeldefare

Reputation: 2303

String foundString = modelList.firstWhere((abc) => abc.id == idToFind).name;

Upvotes: 1

Related Questions