user17336890
user17336890

Reputation:

Flutter: check if a list contains only a certain value

I need to know if there are only string equals to "Validated" in the list, how can I check it in 1 line of code ? (If the list is empty, I already checked the condition before so this particular case isn't important).

List<String> state_str_list = ["Validated", "Draft", "Draft", "Waiting", "Validated"];
if (???) {
    print("all values in state_str_list are equals to 'Validated' !");
}

Upvotes: 4

Views: 7081

Answers (1)

user17336890
user17336890

Reputation:

Thanks to you, I came to this:

contains_only(var _list, var e) {
  _list.every((element) => element == e);
}
print(contains_only(["Validated", "Draft", "Draft", "Waiting", "Validated"], "Validated"));

Upvotes: 6

Related Questions