我有一个API。它包含我想根据其中的特殊字母和ID过滤的数据。两种型号都没有共同的ID。但是我需要当它按下时,用户接收到由ID过滤的数据,并同时检查名称中是否有大写字母a和大写字母B。我正在尝试做这个:
List<Stop> stop = Provider.of<List<Stop>>(context)
.skip(1)
.where((element) =>
element.stTitle.toUpperCase().contains('А') ==
element.stTitle.toUpperCase().contains("A"))
.where((element) =>
element.stTitle.toUpperCase().contains("B") ==
element.stTitle.toUpperCase().contains('B')).where((element) => element.stId == stId)
.toList();
但它给了我空白的屏幕,虽然状态200从服务器,当我删除行:where((element) => element.stId == stId)
,它给出,但当我删除这条线,我只是得到坚实的数据,而不是过滤。我该如何解决这个问题?
模型:
Routes{
final int mrid;
final String mrTitle;}
Stop{
final int stId;
final String stTitle;}
我也试图做这样的事情,它与虚拟数据工作:
1. List <Stop> filtredstop = [];
2. initState(){
filteredstops = where((element) => element.stId == stId)).toList();
}
但是在我的例子中,它给我的不是过滤元素的数组,而是数组中的一个项目。所以我不明白,为什么。
请验证从Provider
返回的列表是否有效并包含项目。
List<Stop> stop = Provider.of<List<Stop>>(context)
使用skip
操作可能导致列表中n
项被排除。我知道你知道这一点。
List<Stop> stop = Provider.of<List<Stop>>(context).skip(1); // Will exclude item at 0 index.
List<Stop> stop = Provider.of<List<Stop>>(context)
.skip(1)
.where((element) =>
element.stTitle.toUpperCase().contains('А') ==
element.stTitle.toUpperCase().contains("A")).where((element) =>
element.stTitle.toUpperCase().contains("B") ==
element.stTitle.toUpperCase().contains('B')).where((element) => element.stId == stId)
.toList();
在上面的代码片段中,你试图过滤包含'A'的列表。并通过检查它是否包含'B'来过滤结果列表。最后验证id
在过滤'B'后的结果列表中.这个过程就像下面的ActualList ->过滤器(' A)→filteredAinTheList→过滤器(B)→filteredListofAandB→Filter()→合成列表。
这里列表项的标题可能是,也可能不是包含字母"A"或"B",以及用"Id"过滤的结束列表可以为空
所以更好的方法是首先用'ID'过滤列表,然后检查它是否有'A'或'B'。下面是示例代码片段:
List<Stop> stopList = [ Stop('Avengers ', 1), Stop('Marvel', 2),
Stop('Tony ', 3), Stop('BDE ', 4), Stop('JOE ', 2)]; // From Provider
final receivedId = 1; // Dynamic ID received in the constructor
final searchText = 'A'; // As per your requirement.
final filteredIdList = stopList.where((item) {
return receivedId == item.id && item.title.contains(searchText);
}).toList();
print(filteredIdList);
那么结果将输出如下
[1 -复仇者联盟]