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

Change item in a List

I’m fetching data from an API that contains details of some person with their job. That could happen that one person has more than one job.

In this case, I don’t want to add this person to a list but I want to append the current job title to the existing one.

Here is the snippet of the code:

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

 if (workers.any((item) => item.id == person.id)) {
     //here I want to append the job title.
      } else {
        workers.add(coWorker);
      }

>Solution :

Maybe you can do something like this:

if (workers.any((item) => item.id == person.id)) {
  final worker = workers.firstWhere((item) => item.id == person.id);
  worker.jobTitle += 'appended text to jobTitle if that is a string';
} else {
  workers.add(coWorker);
}
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