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


Asked by Mark on October 31, 2021 (source).

I have the following 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

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}


Question answered by pr0gramist (source).

Yes, you can iterate over the list with map method and inside it return the new map with the spread operator like this:

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