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.