如何根据多种条件过滤列表?

时间:2020-05-02 11:23:02

标签: arrays list flutter math search

我正在尝试使用多个条件过滤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语句。不严重。但是,当有更多条件时,我将无法执行此操作。幸运的是,这仅是两个条件,但我还有很多其他条件。

还请注意,我在上一条语句中使用了OR命令。这意味着获得homeVisits=trueonTheSpotServices=true

处的结果

处理此问题最有效的方法是什么?

1 个答案:

答案 0 :(得分:2)

无需级联多个Host server1 Match user test* IdentityFile /home/test1/.ssh/id_rsa.test*

相反,使用具有自定义public class App{ Library library = new Library(); //the other stuff private void run(){ library.addGenres(insertGenreNameHere); } } public class Library{ private Genres[] genres = new Genres[5]; //Obj. Array of genres private Int nrOfGenres = 0; //number of how many genres there are in an array public void addGenres(String genreName){ //adds a new genre to the array if (nrOfGenres < genres.length) { genres[nrOfGenres] = new Genres(genreName); nrOfGenres++; } else { System.out.println("You already have the maximum of " + genres.length + " genres!"); } } public class Genres { private String name; private Books[] books = new Books[5]; //Obj. Array of books private int nrOfBooks = 0; //number of how many books there are in an array public Genres(String name) { //Constructor this.name = name; } //getter & setter for the name of the genre public void addBooks(String titel){ //adds new book to the array if (nrOfBooks < books.length) { books[nrOfBooks] = new Books(titel); nrOfBooks++; } else { System.out.println("You already have the maximum of " + books.length + " books!"); } } public void showBooks(){ //prints the books line by line int x = 0; while(x < books.length && books[x] != null) { System.out.println(books[x].getTitle()); x++; } } } public class Books(){ private String title; public Books(String title){ //Constructor this.title = title; } //getter & setter for the title } 函数的单个if-else

where