Home > Net >  Flutter: generate a list in 1 line
Flutter: generate a list in 1 line

Time:10-15

I know how to create a list from others list / map / ... in a simple way. But I want to do it more quickly because I think it's possible to do it in 1 line.

Example of a list generation from a list of map:

List<String> getUserEmailList() {
  List<String> userEmailList = [];
  for (Map<String, dynamic> userDict in globalUserDictList!) {
    userEmailList.add(userDict["email"]);
  }
  return userEmailList;
}

How to generate the userEmailList from my globalUserDictList in 1 line ?

CodePudding user response:

you can do

List<String> userEmailList = globalUserDictList!.map((e) => e["email"]).toList();

CodePudding user response:

final userEmailList = [for (final (e in globalUserDictList) e["email"] ];
  • Related