Home > Net >  Check string exist in array dart / flutter
Check string exist in array dart / flutter

Time:10-06

i have array like this

var data = [manggo, lemon, watermelon, guava];

I want to display watermelon when I pass the string "water", how do I do that? when i use contains, it will return false

CodePudding user response:

how about this:

void main() {
 var data = ["manggo", "lemon", "watermelon", "guava"];

print(data.where((e)=> e.contains("water")).toList());
}

enter image description here

CodePudding user response:

try this.

 void main() {
  
 List<String> data = ["manggo", "lemon", "watermelon", "guava"];
  
 List<String> searchData(String text){
    if(text.isEmpty){
      return data;
    }
    return data.where((element) => element.toLowerCase().contains(text.toLowerCase())).toList();
  }
  
  print(searchData("wa"));
  print(searchData("g"));
  print(searchData("gu"));
}

output:

[watermelon]
[manggo, guava]
[guava]
  • Related