Reputation: 2489
I have a List<MyClass> myClass;
, I want to get List<String> myList;
from all of myClass.item, is there one line way to convert it?
Upvotes: 0
Views: 292
Reputation: 6029
You can use the map method from List class to loop through the entire list. Don't forget to call toList in the end, as Dart's map returns a kind of Iterable. Please see the code below.
class Employee {
int id;
String firstName;
String lastName;
Employee(
this.id,
this.firstName,
this.lastName,
);
}
void main() {
List<Employee> employees = <Employee>[
Employee(1, "Adam", "Black"),
Employee(2, "Adrian", "Abraham"),
Employee(3, "Alan", "Allan"),
];
List<String> myList = employees.map((item)=>item.firstName).toList();
print(myList);
}
Upvotes: 2