0

I have an array <EmailUser>[]

class EmailUser{
final String id;
final String type;
}

I want to iterate over the array and when the condition of (emailUser.id == id) is met I want to give this object a value for the type property.

I've seen firstWhereOrNull from the collections package. But that returns the object. Which would involve removing the object and adding it to the array again.. What his the neatest way to do this..

1 Answer 1

1

First, you cannot assign a new value for type field because it's declared as final.

Do this instead:

class EmailUser {
  int id;
  String type;
  
  EmailUser({
    this.id = 0,
    this.type = '',
  });
}

Then you could do this:

List<EmailUser> list = ...;
...
for (final user in list) {
  if (user.id == id) {
    user.type = value;
  }
}

Update

You can also do this:

void update(EmailUser user) {
  if (user.id == someId) {
    user.type = 'New Value';
  }
}

...

list.forEach(update);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.