I created a task list. I want to count the tasks length like this:
ongoing tasks
= task list length
- isDone tasks
I want to find the isDone
tasks count in this case. As to the picture isDone
tasks = 2.
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;
}
}
CodePudding user response:
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;
}
CodePudding user response:
if something goes wrong, you may need to convert List
to Iterable
int get doneCount {
Iterable<Task> tasksDone = _tasks
.where((task) => task.isDone)
.toList(); //where returns an iterable so we convert it back to a list
return tasksDone.length;
}