This code does a search in a list and returns the item. Depending on the word, using switch case the code becomes longer.
List data = [{"Label": "teXt VariablE sizE", "Value": 1385},{"Label": "Name variable Test", "Value": 1386},{"Label": "sizE Variable sizE", "Value": 1387},{"Label": "sizE Item Size", "Value": 1388}];
List match = [];
String text = "variable size text";
var split = text.toString().split(' ');
switch (split.length) {
case 1:
List<dynamic> list1 = data
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(text.toUpperCase())))
.toList();
match = list1;
break;
case 2:
String one = split[0];
String two = split[1];
List<dynamic> list1 = data
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(one.toUpperCase())))
.toList();
List<dynamic> list2 = list1
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(two.toUpperCase())))
.toList();
match = list2;
break;
case 3:
String one = split[0];
String two = split[1];
String three = split[2];
List<dynamic> list1 = data
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(one.toUpperCase())))
.toList();
List<dynamic> list2 = list1
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(two.toUpperCase())))
.toList();
List<dynamic> list3 = list2
.where((oldValue) => (oldValue['Label']
.toString()
.toUpperCase()
.contains(three.toUpperCase())))
.toList();
match = list3;
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
}
print(match);
//return search result
CodePudding user response:
List filter(List data, String text) {
List match = [];
for (var m in text.toString().split(' ')) {
match.addAll(
data.where((oldValue) => oldValue['Label']
.toString()
.toUpperCase()
.contains(m.toUpperCase())));
}
return match;
}
void main() {
String text = "variable size text";
//text = "abc";
List data = [{"Label": "abc", "Value": 1385}, {"Label": "teXt VariablE sizE", "Value": 1385},{"Label": "Name variable Test", "Value": 1386},{"Label": "sizE Variable sizE", "Value": 1387},{"Label": "sizE Item Size", "Value": 1388}];
print(filter(data, text));
}
CodePudding user response:
Felipe your code is hard to read and not going to say bad but not the best way to achieve what you want I'm going to write an what we can say an enhanced version to achieve that
simply the way will be by sorting the two strings and compare their values
first, let's split the string by word like you did in your question
List<String> splittedText = text.split(" ");
now we sort it
splittedText.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
//before sorting
[variable, size, text]
//after sorting
[size, text, variable]
now we just loop the list of data to check evrey string and compare it to our text string.
//Same as splittedText but for our data list
List<String> listOfStringValue =[];
//looping evrey index in the list
data.forEach((data){
//clearing the list to be ready for the new string
listOfStringValue =[];
// same logic before in text.split(" "); and the sort
listOfStringValue = data["Label"].split(" ");
listOfStringValue.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
//compare the text in the data to our text if it's the same add it to match list
if(listOfStringValue.toString().toLowerCase() == splittedText.toString().toLowerCase()){
match.add(data);
}
});
here is the full code as a function
void main() {
List<Map<String,dynamic>> data = [{"Label": "teXt sizE VariablE", "Value": 1385},{"Label": "teXt VariablE sizE", "Value": 1385},{"Label": "Name variable Test", "Value": 1386},{"Label": "sizE Variable sizE", "Value": 1387},{"Label": "sizE Item Size", "Value": 1388}];
String text = "variable size text";
print(findTheText(data,text));
}
List findTheText(List<Map<String,dynamic>> data,String text){
List match = [];
List<String> listOfStringValue =[];
List<String> splittedText = text.split(" ");
splittedText.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
data.forEach((data){
listOfStringValue.clear();
listOfStringValue = data["Label"].split(" ");
listOfStringValue.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
if(listOfStringValue.toString().toLowerCase() == splittedText.toString().toLowerCase()){
match.add(data);
}
});
return match;
}