Unexpected result list add from map

 With this code I get three lines with the same id (3). Why? 
 Thanks for your help

  var map = new Map();
  final list = [];


Hello and welcome! I’m not quite sure to understand what you are trying to do here, but if you want to dynamically build a list of maps like:

[{id: 1}, {id: 2}, {id: 3}]

this is a functional way to do it:

void main() {
  const ids = [1, 2, 3];

List idsMapList(ids) {
  return ids.map((id) => {'id': id}).toList();

For what concerns the reason why your code does not produce the result you expect, I’m not a Dart expert, actually a newbie myself, but I suspect it has to do with Dart being a lexically scoped language (which the language tour explains this way):

[…] which means that the scope of variables is determined statically, simply by the layout of the code.

So, for what I understand map will be assigned the value {id: 3}, being it the last value you assigned to it, and that value is then used in all three list.add(map) operations you perform. But maybe some more expert Dart developer can correct me, and share a better explanation.
I’ve also found the following example which could help to see the difference with Javascript behavior



here you are providing value 1 to key ‘id’ of map and after


[{id: 1}]

here you are providing value 2 to key ‘id’ of map which will overwrite previous value,
will finally store after ```

`[{id: 2},{id: 2}]`

and at last after 
we have
`[{id: 3}, {id: 3}, {id: 3}]`