【发布时间】:2020-08-16 21:23:58
【问题描述】:
我正在尝试过滤具有多个条件的List。主要的是,如果它是true,我必须使用条件,而不是false。如果条件是false,我不应该使用它来过滤。下面是我的代码
void performFiltering(bool homeVisits, bool onTheSpotServices)
{
//Check for home and on the spot filtering
if(homeVisits==true)
{
filteredOrganizationList = orgList.where((org) => org.homeVisits==true);
}
else if(onTheSpotServices==true)
{
filteredOrganizationList = orgList.where((org) => org.onTheSpotService==true);
}
else if(homeVisits==true && onTheSpotServices==true)
{
filteredOrganizationList = orgList.where((org) => (org.onTheSpotService==true) ||(org.homeVisits==true) );
}
}
在这里我做了简单的if-else 声明。不严重。但是当有更多的条件时,我不能这样做。幸运的是,这只是 2 个条件,但我还有更多条件要做。
还要注意我在最后一条语句中使用了OR 命令。这意味着获得homeVisits=true 或onTheSpotServices=true 的结果
最有效的处理方法是什么?
【问题讨论】:
-
orgList.where((org) => homeVisits && org.homeVisits || onTheSpotServices && org.onTheSpotService || ... etc etc) -
@pskink:太好了。请提供您的评论作为答案
标签: arrays list flutter math search