Home > Enterprise >  Flutter: Find element
Flutter: Find element

Time:12-05

How can I find the element if my list is like this:

toDoList = [
      ["Make Tutorial", false],
      ["Do Exercise", false],
    ];

And the method that I want to use is this:

results = db.folderTask
          .where((folder) =>
              folder.name!.toLowerCase().contains(enteredKeyword.toLowerCase()))
          .toList();

What I want to do is to search the task by using the method that I mentioned above.

CodePudding user response:

To search for an element in a list of lists using the where method, you can use a nested where clause to filter each sublist. Here is an example of how you could do this:

results = db.folderTask
  .where((folder) => folder.toDoList.where((task) => task[0].toLowerCase().contains(enteredKeyword.toLowerCase()))
  .toList();

This code uses a nested where clause to filter each sublist in the toDoList property of the folder object. The inner where clause filters each sublist based on the first element of the sublist (the task name), and checks if it contains the enteredKeyword. The outer where clause then filters the folder object based on whether the inner where clause returned any results.

This will return a list of folder objects that have at least one task in the toDoList property whose name contains the enteredKeyword.

  • Related