此代碼在串列中進行搜索并回傳專案。根據單詞,使用 switch case 代碼會變得更長。
串列資料 = [{“標簽”:“文本變數大小”,“值”:1385},{“標簽”:“名稱變數測驗”,“值”:1386},{“標簽”:“大小變數大小E” , "值": 1387},{"標簽": "sizeE 專案大小", "值": 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);
//回傳搜索結果
uj5u.com熱心網友回復:
費利佩你的代碼很難閱讀,不會說不好,但不是實作你想要的最好的方法我要寫一個我們可以說的增強版本來實作這一點
簡單的方法是對兩個字串進行排序并比較它們的值
首先,讓我們像您在問題中所做的那樣逐字拆分字串
List<String> splittedText = text.split(" ");
現在我們對其進行排序
splittedText.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
//before sorting
[variable, size, text]
//after sorting
[size, text, variable]
現在我們只需回圈資料串列以檢查 evrey 字串并將其與我們的文本字串進行比較。
//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);
}
});
這是作為函式的完整代碼
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;
}
uj5u.com熱心網友回復:
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));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/465099.html
