how to get value of item in array of list and make them independent list

Viewed 239

I have list like example below, I want to get value of nameCategory and make them to List

List<Categories> iconCategory= [
  Categories(
    iconPath: 'assets/images/icons/IncomeIcon/001-salary.svg',
    nameCategory: 'Salary',
  ),
  Categories(
    iconPath: 'assets/images/icons/IncomeIcon/002-interest.svg',
    nameCategory: 'Interest',
  ),
  Categories(
    iconPath: 'assets/images/icons/IncomeIcon/003-award.svg',
    nameCategory: 'Award',
  )];

to list like example below but the data is from array list above how I can do that

List<String> myCategory = ['Salary','Interest','Award'];
2 Answers

You can simply use the map method to achieve this:

List<String> myCategory = mycategories.map((e) => e.nameCategory).toList();
print(myCategory); // Prints [Salary, Interest, Award]

this will give you what you need

List<Categories> s1 = [];
List<String> ss = [];
result!.map((e) {
  ss.add(e.nameCategory);
});
Related