In Dart.

I have this list which is contains the data of this JSON:

[{title: item1., image: image1, url: https://www.google.es/}, {title: noticia 2, description: Descripción 2, image: image2, url: https://www.sport.es/}, {title: noticia 1, description: Lorem iptsum lerem lorem iptsun lorem iptsun lorem, image: image3, url: https://www.ole.com.ar/}, {title: noticia 6, description: Lorem iptsum lerem lorem iptsun lorem iptsun lorem, image:image4, url: https://www.ole.com.ar/}]

So I have a map that I am trying to add to the list.

 Map<String, dynamic> newElement = {};
    newmap['title'] = _title;
    newmap['description'] = _description;
    newmap['image'] = _imageUrl;
    newmap['url'] = 'www.google.com';

However, when I use list.add(), the first time it adds the item correctly. On the second insert the problem occurs, the previously added element takes the value of the last added element, that is, it is overwritten. How I can avoid this?

Example:

void main() {
  List<dynamic> finalData = [{'title': 'a ', 'description': 'b.', 'image': 'ccc2', 
                              'url': 'https://www.google.es/'}];

  print(finalData[0]);

  Map<String,dynamic> ne = {};

  ne['title'] = 'hola';
  ne['description'] = 'hola';
  ne['image'] = 'hola';
  ne['url'] = 'www.google.com';

  print(ne);

  finalData.add(ne);

  print(finalData);

  ne.clear();

  ne['title'] = 'hol2a';
  ne['description'] = 'hol2a';
  ne['image'] = 'ho2la';
  ne['url'] = 'www.googl2e.com';

  print(ne);

  finalData.add(ne);

  print(finalData);

}

This example runs on Dartpad.

The data type of the list is _JsonMap.


Solution 1: salihgueler

The reason comes from the fact that how object allocation works in Object Oriented Programming and languages that works with it.

When you have an object that is created, you work with that objects reference. So you add it to the list as a reference as well. After you update that reference outside the list, the reference inside it still changes. Proper way to do it would be in two ways, either creating a new object below

void main() {
  List<dynamic> finalData = [{'title': 'a ', 'description': 'b.', 'image': 'ccc2', 
                              'url': 'https://www.google.es/'}];

  print(finalData[0]);

  Map<String,dynamic> ne = {};

  ne['title'] = 'hola';
  ne['description'] = 'hola';
  ne['image'] = 'hola';
  ne['url'] = 'www.google.com';

  print(ne);

  finalData.add(ne);

  print(finalData);

  Map<String,dynamic> ne2 = {};

  ne2['title'] = 'hol2a';
  ne2['description'] = 'hol2a';
  ne2['image'] = 'ho2la';
  ne2['url'] = 'www.googl2e.com';

  print(ne2);

  finalData.add(ne2);

  print(finalData);

}

Or assign the value by giving it a new reference like below:

void main() {
  List<dynamic> finalData = [{'title': 'a ', 'description': 'b.', 'image': 'ccc2', 
                              'url': 'https://www.google.es/'}];

  print(finalData[0]);

  Map<String,dynamic> ne = {};

  ne['title'] = 'hola';
  ne['description'] = 'hola';
  ne['image'] = 'hola';
  ne['url'] = 'www.google.com';

  print(ne);

  finalData.add(ne);

  print(finalData);

  ne = {};

  ne['title'] = 'hol2a';
  ne['description'] = 'hol2a';
  ne['image'] = 'ho2la';
  ne['url'] = 'www.googl2e.com';

  print(ne);

  finalData.add(ne);

  print(finalData);

}