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

how to update a list used as a value for a map in dart

I have been trying to update a Map<String, List> sender like I would a normal map, and I haven’t had any luck. I looked at this question but trying to add a [0] to mark the index doesn’t work. I keep getting a Script error so when the list isn’t even being displayed in my UI, I can’t trace why. I want sender to be
{"Header" : [content]}
then to_repo is called again
{"Header" : [content, content]}

void to_repo(content) {
    if (!sender.containsKey('Header')) {
  sender['Header'][0] = content;
}
  else if (sender.containsKey('Header')) {  sender.update(
       "Header",
         (value) => value.add(content.toString()),
       
      // ifAbsent: () => content.toString(),
       );
                                          }
    

>Solution :

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

Instead of

sender['Header'][0] = content;

you can do

sender['Header'] = [content];

EDIT:

I believe you could also rewrite the function to this:

void to_repo(content) {
  sender['Header'] = (sender['Header'] ?? [])..add(content);
}
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