Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count a specific length of a list in flutter

I created a task list. I want to count the tasks length like this:

ongoing tasks = task list lengthisDone tasks

I want to find the isDone tasks count in this case. As to the picture isDone tasks = 2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sample image

These are the classes that I created.

class Task {
  final String taskName;
  bool isDone;

  Task({required this.taskName, this.isDone = false});

  void toggleDone() {
    isDone = !isDone;
  }
}

……..

class Taskdata with ChangeNotifier {
  final List<Task> _tasks = [];

  UnmodifiableListView<Task> get tasks {
    return UnmodifiableListView(_tasks);
  }

  int get tasksCount {
    return _tasks.length;
  } 
  
}

>Solution :

You can use the where function to filter a list per a certain condition:

 int get tasksCount {
    List<Task> tasksLeft = _tasks.where((task) => !task.isDone)).toList();  //where returns an iterable so we convert it back to a list
    return tasksLeft.length;  
  } 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading