Putting it together
- Dart course
- Beginner
- Lesson 87/116
- 6 min
Build a working CLI app with a menu loop.
Putting it together
All pieces are now ready: the Task class, the TodoList with its addTask, listTasks and completeTask methods. In this lesson we call them all from main to get a working CLI.
A typical session: add a few tasks, list them, complete one, list again. We mimic this flow by writing the calls in order inside main. A real CLI would read from stdin; for now we begin with fixed calls.
In the final task you will add two tasks, complete one and print the list. This shows every piece of the project together.
A working session
Now we write a full session. In main we first build our list with var list = TodoList();. Then we add two tasks, list them, complete one and list again.
This flow mimics a user's typical interaction. A real CLI reads commands from the user with stdin.readLineSync; for now we use fixed code. This keeps the project small and predictable.
The output prints the task list at each listTasks call. This lets you see the effect of adding and completing. In later courses we will extend this CLI.
Practice Working CLI
Inside main create a TodoList. Add two tasks with list.addTask('Kitap oku') and list.addTask('Spor yap'). Complete the first task with list.completeTask(0). Finally add a list.listTasks() call.
Starter code
class Task {
String title;
bool done = false;
Task(this.title);
}
class TodoList {
List<Task> tasks = [];
void addTask(String title) {
tasks.add(Task(title));
}
void listTasks() {
for (var i = 0; i < tasks.length; i++) {
print('${i + 1}. ${tasks[i].title}');
}
}
void completeTask(int i) {
tasks[i].done = true;
}
}
void main() {
// add two tasks, complete one, list
}Sample solution
void main() {
var list = TodoList();
list.addTask('Kitap oku');
list.addTask('Spor yap');
list.completeTask(0);
list.listTasks();
}Expected output
1. Kitap oku 2. Spor yap
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which order makes sense?
- list → add → complete
- complete → add → list
- add → complete → add
- add → list → complete → list Correct answer
Explanation: first add, then list, then complete, finally list again.
2 Match each method to its purpose
| Term | Meaning |
|---|---|
addTask |
Add a task |
listTasks |
List |
completeTask |
Complete |
Task |
Single task |
3 Add two tasks
void main() {
var list = TodoList();
list.addTask(____);
list.addTask('Spor yap');
list.listTasks();
}
void main() {
var list = TodoList();
list.addTask('Read a book');
list.addTask('Spor yap');
list.listTasks();
}
Explanation: the string goes inside single quotes.