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 – Update List items including condition

I want to change the items of a list whose variable seen is false,

my model:

class Message{
  String text;
  bool seen;
  Message(this.text, this.seen)
}

my list:

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

List<Message> messages = [

Message('hi', false),
Message('hi2', true),
Message('hi4', false),
Message('hi6', false),
Message('hi8', true),

];

>Solution :

To change the items of the messages list whose seen variable is false, you can iterate through the list and use a conditional statement to check if the seen variable is false. If it is false, you can modify as you need.

Here’s an example code snippet to achieve this:

for (var message in messages.where((element) => element.seen == false)) {
  message.seen = true;
}
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