Dart Lesson 48 – CLI Tools Project | Dataplexa

Dart CLI Tools Project

In this lesson, you will build a real-world Command Line Interface (CLI) application using Dart.

CLI tools are widely used for automation, scripting, data processing, DevOps tasks, and developer utilities.


What Is a CLI Tool?

A CLI tool is a program that runs in the terminal and interacts with users through text-based input and output.

Examples of CLI tools include:

  • Git
  • Docker
  • NPM
  • Dart & Flutter commands

Project Overview

We will build a Task Manager CLI Tool that can:

  • Add tasks
  • View all tasks
  • Save tasks to a file
  • Load tasks from a file

All tasks will be stored in a local text file.


Project Structure

Our project will use the following file:

  • tasks.txt — stores task data

Import Required Libraries

import 'dart:io';

Displaying the Menu

The menu allows users to choose an action.

void showMenu() {
  print("\n=== Task Manager ===");
  print("1. Add Task");
  print("2. View Tasks");
  print("3. Exit");
  stdout.write("Enter choice: ");
}

Adding a Task

This function accepts user input and appends it to the file.

void addTask() {
  stdout.write("Enter task description: ");
  String? task = stdin.readLineSync();

  if (task != null && task.isNotEmpty) {
    File file = File('tasks.txt');
    file.writeAsStringSync(task + '\n', mode: FileMode.append);
    print("Task added successfully");
  } else {
    print("Invalid task");
  }
}

Viewing Tasks

This function reads and displays all tasks from the file.

void viewTasks() {
  File file = File('tasks.txt');

  if (!file.existsSync()) {
    print("No tasks found");
    return;
  }

  List tasks = file.readAsLinesSync();
  print("\nYour Tasks:");

  for (int i = 0; i < tasks.length; i++) {
    print("${i + 1}. ${tasks[i]}");
  }
}

Main Program Loop

This loop keeps the program running until the user exits.

void main() {
  while (true) {
    showMenu();
    String? choice = stdin.readLineSync();

    switch (choice) {
      case '1':
        addTask();
        break;
      case '2':
        viewTasks();
        break;
      case '3':
        print("Exiting Task Manager");
        return;
      default:
        print("Invalid choice");
    }
  }
}

How the Project Works

  • User selects an option
  • Program executes the selected function
  • Tasks are saved permanently in a file
  • Program runs until user exits

Sample Output

=== Task Manager ===
1. Add Task
2. View Tasks
3. Exit
Enter choice: 1
Enter task description: Finish Dart lesson
Task added successfully

Why This Project Is Important

This CLI project demonstrates:

  • Real user interaction
  • Persistent data storage
  • Control flow with loops and conditions
  • Practical Dart usage beyond syntax

📝 Practice Extensions

Extension 1

Add an option to delete a task.

Extension 2

Add task priorities (High, Medium, Low).

Extension 3

Save completed tasks in a separate file.


What’s Next?

In the next lesson, you will build a complete Web App Project using Dart concepts for real-world applications.