Home > other >  Dart - How do I make detect If variable value is exist in a list
Dart - How do I make detect If variable value is exist in a list

Time:12-28

How do I make detect if variable data is a list or not? It always giving me an error The argument type 'List<String>' can't be assigned to the parameter type 'Pattern'. Can someone help me out?

var lister = ["Hello", "There!"];
var data = "There";

void main() {
  if (data.contains(lister)) {
    print("Yeah, it's in a list");
  } else {
    print("Nope, it's not in a list");
  }
}

CodePudding user response:

try the following

var lister = ["Hello", "There!"];
var data = "There";

void main() {
  int? itemIndex = lister.indexWhere((item) => item.contains(data));

  if (itemIndex > 0) {
    print("Yeah, it's in a list");
  } else {
    print("Nope, it's not in a list");
  }
}
  •  Tags:  
  • dart
  • Related