简体   繁体   中英

Update value of a single entry in a List<Map<String, dynamic>> in dart

I have a list of type Map<String,dynamic> , I fill this list with List.filled .

[{gstatus: 1, vermerk: }, {gstatus: 1, vermerk: }, {gstatus: 1, vermerk: }, {gstatus: 1, vermerk: }]

But if I want to change eg the value "vermerk" of the second entry of the list with ".update()", all values of the key "vermerk" change like:

[{gstatus: 1, vermerk: test}, {gstatus: 1, vermerk: test}, {gstatus: 1, vermerk: test}, {gstatus: 1, vermerk: test}]  

I would like to have the following result

[{gstatus: 1, vermerk: }, {gstatus: 1, vermerk: test}, {gstatus: 1, vermerk: }, {gstatus: 1, vermerk: }]  

Here the Code

void main() {
  
  late List<Map<String, dynamic>> deviceForm;
  
  deviceForm = List.filled(4,
          {"gstatus": 1, "vermerk": ""});
  
  print(deviceForm);
  
  deviceForm[1].update('vermerk', (value) => 'test');
  
  print(deviceForm);
  
}

You can try to use generate and update


void main() {
  
  List<Map<String, dynamic>> deviceForm = List.generate(4,
         (index)=> {"gstatus": 1, "vermerk": ""});
  
  print(deviceForm);
  
  deviceForm[1].update('vermerk', (value) => 'test');
  
  print(deviceForm);
  //Output : [{gstatus: 1, vermerk: }, {gstatus: 1, vermerk: test}, {gstatus: 1, vermerk: }, {gstatus: 1, vermerk: }]
}

This is very interesting issue, seems like List.filled will produce identical data, because of this somehow every data will be changed if you try to change one of them... and once i check like this, it return TRUE

print(deviceForm[1] == deviceForm[2]); //TRUE

but i try another shot with List.generate, the result will be different

print(deviceForm[1] == deviceForm[2]); //FALSE

If you have specific reason why you use List.filled, maybe this answer will be useless, or in case you have no problem to switch using List.generate, it will solve your problem

deviceForm = List.generate(4, (index) => {"gstatus": 1, "vermerk": ""});

List.filled : All elements of the created list share the same [fill] value .

The issue lies on List.filled , you can use List.generate instead

  deviceForm1 = List.generate(
    3,
    (_) => {"gstatus": 1, "vermerk": ""},
  );

Or create new map and assign on index 1.

void main() {
  late List<Map<String, dynamic>> deviceForm;

  deviceForm = List.filled(2, {"gstatus": 0, "vermerk": ""});

  print(deviceForm[1] == Map.from(deviceForm[0])); //false
  print(deviceForm[1] == deviceForm[0]); //true

  deviceForm[1] = Map.from(deviceForm[1])
    ..update("vermerk",
        (value) => "Test"); // we are creating new Map and assingng it

  print(deviceForm);
  //[{gstatus: 0, vermerk: }, {gstatus: 0, vermerk: newValue}]
}

And check these questions

I am taking some parts

like many other languages (eg Java, Python), is technically pass-by-value where the "value" of an object is a reference to it. This is why functions can mutate arguments passed by the caller. However, in these languages where everything is an object and where there are not separate pointer types, "pass-by-value" and "pass-by-reference" can be confusing terms and are not particularly meaningful. A lot of Python programmers instead use the term pass-by-assignment ..ReadMore

The pass-references-by-value behavior is common to a lot of modern languages (Smalltalk, Java, Ruby, Python…). When you pass an object, you're actually passing-by-value (therefore copying) the contents of your variable, which is a pointer to the object. You never control where objects really exist. ...ReadMore

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM