简体   繁体   English

如何在 flutter/dart 中向列表项添加新属性?

[英]How can I add an new property to a list item in flutter / dart?

I have the following List我有以下List

[
{thumb: 28986/H6a6Xkm_thumb.jpeg, full: 28986/H6a6Xkm.jpeg}, 
{thumb: 28986/f-2_thumb.jpg, full: 28986/f-2.jpg}, 
{thumb: 28986/Ft_thumb.jpg, full: 28986/Ft.jpg}, 
{thumb: 28986/FED_thumb.jpg, full: FED.jpg}
]

I wanted to try to add a new property to each of these 4 items called desc which would be a description, lets say for now: Apple我想尝试为这 4 个项目中的每一个添加一个名为desc的新属性,这将是一个描述,现在可以说: Apple

Is there an easy way to iterate the list and insert the new property so the final result would be:是否有一种简单的方法来迭代列表并插入新属性,以便最终结果是:

[
{thumb: 28986/H6a6Xkm_thumb.jpeg, full: 28986/H6a6Xkm.jpeg, desc: Apple}, 
{thumb: 28986/f-2_thumb.jpg, full: 28986/f-2.jpg, desc: Apple}, 
{thumb: 28986/Ft_thumb.jpg, full: 28986/Ft.jpg, desc: Apple}, 
{thumb: 28986/FED_thumb.jpg, full: FED.jpg, desc: Apple}
]

Is your list a list of maps?您的列表是地图列表吗?

The easiest way to edit every value in a list is using the .map method:编辑列表中每个值的最简单方法是使用.map方法:

myList = myList.map((value) => newValue).toList();

so for example say you have a list of numbers and want to add one to each of them例如,假设您有一个数字列表,并希望为每个数字添加一个

myList = myList.map((number) => number+1).toList();

you can also pass more calculations like so:您还可以像这样传递更多计算:

myList = myList.map((value) {
  // some calculations
  return newValue;
}).toList();

Here is how to solve your problem with that code assuming your list is List<Map<String, dynamic>>假设您的列表是List<Map<String, dynamic>>以下是如何使用该代码解决您的问题

myList = myList.map((value) => value..addAll({'desc': 'apple'})).toList();

Yes, you can iterate over the list with map method and inside it return the new map with the spread operator like this:是的,您可以使用map方法遍历列表,并在其中使用扩展运算符返回新地图,如下所示:

final myList = [{"hello": "world"}];
final newList = myList.map((obj) => {...obj, "desc": "Apple"}).toList();

You can use an for loop or the forEach list property to add an element to the lists:您可以使用 for 循环或 forEach 列表属性将元素添加到列表中:

void main() async {
  var list = [
    {'thumb': '28986/H6a6Xkm_thumb.jpeg', 'full': '28986/H6a6Xkm.jpeg'},
    {'thumb': '28986/f-2_thumb.jpg', 'full': '28986/f-2.jpg'},
    {'thumb': '28986/Ft_thumb.jpg', 'full': '28986/Ft.jpg'},
    {'thumb': '28986/FED_thumb.jpg', 'full': 'FED.jpg'}
  ];

   /// using for loop
  for (int i = 0; i < list.length; i++) list[i]['desc_for'] = 'Apple';
  print(list);
  
  /// Using forEach 
  list.forEach((item) {
    item['desc_forEach'] = 'Apple';
  });
  print(list);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 和 Dart - 如何仅从列表中复制一个项目并将此项目添加到另一个列表中? - Flutter and Dart - how can I copy only a Item from a list and add this Item to another list? 如何将 Flutter 和 Dart 列表与 Class 属性结合起来? - How can I combine Flutter & Dart List with Class Property? 如何将新项目添加到 map 列表以及如何删除 Flutter 中的项目 - How can I add a new item to a list of map and how to remove a item in Flutter Flutter/Dart/Firestore:如何将地图列表添加到 firebase? - Flutter/Dart/Firestore: How can I add a list of maps to firebase? 如何转换列表<Item>到地图<Datetime, List<Item> &gt; 在飞镖/颤振中 - How can I convert List<Item> to Map<Datetime, List<Item>> in dart / Flutter 如何在列表中添加新项目? - How can I add new item in a list? Flutter Dart:如何更改类列表中 class 属性的值 - Flutter Dart: how can I change the value of a class property in a list of Classes 如何在运行时在 DropDownMenuItems 列表中添加新项目? - How i can add new item in list of DropDownMenuItems at runtime? 如何将 displayName 添加到 Firebase 用户? (颤振/飞镖) - How can I add the displayName to the Firebase user? (Flutter/Dart) 如何在 flutter dart 的字符串中添加通配符? - How can I add Wildcard to the string in flutter dart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM