Home > Net >  How to check string existance in string arraylist in flutter?
How to check string existance in string arraylist in flutter?

Time:10-08

RxList<String> selectedMemberList = <String>[].obs;
selectedMemberList = [180,160,150]

I want to display text in red color if ID contains in selectedMemberList.

So, I simply called,

if selectedMemberList.contains('180'){
text in red color
}else{
text in black color}

but, I got text in black color every time. That means ID doesnt matched. How do I match member ID to given selectedMemberList.

CodePudding user response:

if your items are in a single string separated by a comma, first convert that into a list like this :

String collectedIds = '180','160','150';
final split = tagName.split(',');
final Map<int, String>  selectedMemberList= {
  for (int i = 0; i < split.length; i  )
    i: split[i]
};

and now you can check the condition :

if selectedMemberList.contains('180'){
text in red color
}else{
text in black color}

Hope this will help you.

CodePudding user response:

Here is a cleaner way on checking if value exist on a list.

RxList<String> selectedMemberList = <String>[].obs;
selectedMemberList = ["160","180","150"];
  
  
if (selectedMemberList.where((item) => item == "180").isNotEmpty){
  text in red color
} else {
  text in black color
}
  • Related