Loop through a list of custom type into another list of objects

45 Views Asked by At

Any efficient way (simplify the code or not using a method but a variable expression) for the same result?

static List<Tab> myTabs() {
List<String> des = customType.map((e) => e.description).toList() ;
List<Tab> result = [];
for (var d in des) {
  result.add(Tab(text: d));
}
return result;
}
1

There are 1 best solutions below

2
MendelG On BEST ANSWER

You can do something like this. Just directly adding the Tab widget to a list

static List<Tab> myTabs() {
  return customType.map((e) => Tab(text: e.description)).toList();
}

Or even use the fat arrow (=>) syntax:

static List<Tab> myTabs() => customType.map((e) => Tab(text: e.description)).toList();