Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dart: How to add a new key value pair in object of list?

List<Map<String, dynamic>> list1 = [
    {
      'value': 'Value number one',
    },
    {
      'value': 'Value number two',
    },
  ];


I want to add a new pair "item":0,
Then should be show like this

[
    {
      'value': 'Value number one',
      'item': 0,

    },
    {
      'value': 'Value number two',
      'item': 0,
    },
  ];

I dont know how to meke it this functionality.
Help me,Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Loop through the list and add the new entries.

For-loop approach:

for (final map in list1) {
  map['item'] = 0;
}

Functional approach:

list1.forEach((map) => map['item'] = 0);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading