flutterloopsdartcustom-type

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


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

Solution

  • 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();