Home > Blockchain >  Change item in a List
Change item in a List

Time:08-30

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:

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

CodePudding user response:

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);
}
  • Related