I want to search an item from nested list. I've to find an item from multiple list. Here's the code example that I've tried. But it's not working. I can't identify where's the problem.

 void searchPeople(String query) {

    final searchLower = query.toLowerCase();
    final peopleList = _peopleList.where((catDesc) {
      final subCategoryLower = catDesc.subCategoryList!.where((subCatDesc) {
        final pp = subCatDesc.defList!.where((element) {
          return element.defCode!.toString().contains(searchLower) || element.defDesc!.toLowerCase().contains(searchLower);
        }).toList();

        return subCatDesc.subCategoryCode!.toString().contains(searchLower) || subCatDesc.subCategoryDesc!.toLowerCase().contains(searchLower);
      }).toList();

      return catDesc.categoryDesc!.toString().contains(searchLower) || catDesc.categoryCode!.toString().contains(searchLower);
    }).toList();

    setState(() {
      this.query = query;
      this._peopleList= peopleList ;
    });
  }


Solution 1: Ankit Kumar Maurya

Try this one:

 void searchPeople(String query) {

   int i=-1,j=-1;

    final searchLower = query.toLowerCase();
    final peopleList = _peopleList.where((catDesc) {

      final subCategoryLower = catDesc.subCategoryList!.where((subCatDesc) {

        final pp = subCatDesc.defList!.where((element) {
          return element.defCode!.toString().contains(searchLower) || element.defDesc!.toLowerCase().contains(searchLower);
        }).toList();

        j++;//j only for inner loop
        return pp[j].subCategoryCode!.toString().contains(searchLower) || pp[j].subCategoryDesc!.toLowerCase().contains(searchLower);
      }).toList();

      i++;//i only for outer loop
      return subCategoryLower[i].categoryDesc!.toString().contains(searchLower) || subCategoryLower[i].categoryCode!.toString().contains(searchLower);
    }).toList();

    setState(() {
      this.query = query;
      this._peopleList= peopleList ;
    });
  }

NOTE: I thing the idea of using a nested search as in your problem is not a good way because, since where will run for every element and so, each time it will create a new list(inner filter logic) which will result in higher time complexity of the logic. A better option is to filter sequentially.