【问题标题】:how to get value of item in array of list and make them independent list如何获取列表数组中项目的值并使它们成为独立列表
【发布时间】:2021-09-26 08:36:27
【问题描述】:

我有如下示例的列表,我想获取 nameCategory 的值并将它们添加到列表中

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',
  )];

列出如下示例,但数据来自上面的数组列表 我该怎么做呢

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

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    您可以简单地使用map 方法来实现:

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

    【讨论】:

      【解决方案2】:

      这会给你你所需要的

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

      【讨论】:

      • 感谢您的回答,但我需要将所有索引的所有值转换为列表
      猜你喜欢
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多