Home > Software design >  How do I update each element in a list using Dart?
How do I update each element in a list using Dart?

Time:11-10

I was wondering if there was a possible way to update data of each elements in the list

This is my initial list,

var list1 = ['image1.png', 'image2.png', 'image3.png']

I want the final list in such a way that I could update the all the elements in the list like, (just add a string at the beginning of the elements)

var list1 = ['bucket/image1.png', 'bucket/image2.png', 'bucket/image3.png]

CodePudding user response:

You can like this:

List result = list1.map((e) => 'bucket/$e').toList();

CodePudding user response:

you can do it like this either

var list1 = ['image1.png', 'image2.png', 'image3.png'];
list1 = list1.map((item) => 'bucket/$item').toList();

CodePudding user response:

If you want to mutate the original List instead of creating a new List, just use a loop:

var list1 = ['image1.png', 'image2.png', 'image3.png'];
for (var i = 0; i < list1.length; i  = 1) {
  list1[i] = 'bucket/${list1[i]}';
}
  • Related