Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何将类列表的成员转换为列表<;字符串>;一行代码?_List_Flutter - Fatal编程技术网

List 如何将类列表的成员转换为列表<;字符串>;一行代码?

List 如何将类列表的成员转换为列表<;字符串>;一行代码?,list,flutter,List,Flutter,我有一个列表myClass,我想获取列表myList从所有myClass.item中,是否有一种转换它的行方法?您可以使用map方法从List类循环整个列表。最后别忘了给toList打电话,因为Dart的地图会返回一种Iterable。请参阅下面的代码 class Employee { int id; String firstName; String lastName; Employee( this.id, this.firstName, this.las

我有一个
列表myClass,我想获取
列表myList
从所有myClass.item中,是否有一种转换它的行方法?

您可以使用map方法从List类循环整个列表。最后别忘了给toList打电话,因为Dart的地图会返回一种Iterable。请参阅下面的代码

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);
}
class员工{
int-id;
字符串名;
字符串lastName;
雇员(
这个身份证,
这个名字,
这个姓,
);
}
void main(){
员工名单=[
雇员(1,“亚当”,“黑人”),
雇员(2,“阿德里安”、“亚伯拉罕”),
雇员(3,“艾伦”、“艾伦”),
];
List myList=employees.map((item)=>item.firstName.toList();
印刷品(myList);
}

您可以使用List类中的map方法在整个列表中循环。最后别忘了给toList打电话,因为Dart的地图会返回一种Iterable。请参阅下面的代码

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);
}
class员工{
int-id;
字符串名;
字符串lastName;
雇员(
这个身份证,
这个名字,
这个姓,
);
}
void main(){
员工名单=[
雇员(1,“亚当”,“黑人”),
雇员(2,“阿德里安”、“亚伯拉罕”),
雇员(3,“艾伦”、“艾伦”),
];
List myList=employees.map((item)=>item.firstName.toList();
印刷品(myList);
}

read
List.map
官方文档(和
Iterable
一般文档)read
List.map
官方文档(和
Iterable
一般文档)如果我只需要
[“亚当”、“阿德里安”、“艾伦”]
?更新我的答案请查看上面的代码。如果我只需要
[“Adam”、“Adrian”、“Alan”]
?更新我的答案请查看上面的代码。