Dart Lesson 3 – Dart Basics | Dataplexa

Dart Basics

In this lesson, you will learn the fundamental building blocks of the Dart language. Understanding these basics is essential before moving into variables, functions, and object-oriented programming.

Dart is designed to be simple, readable, and fast, making it ideal for both beginners and large-scale applications.


Your First Dart Program

Every Dart program starts with a main() function. This function is the entry point where program execution begins.

void main() {
  print("Hello, Dart!");
}

Here:

  • void means the function returns nothing
  • main() is the starting point of the program
  • print() displays output to the console

Understanding the print() Function

The print() function is used to display text or values in the console. It is commonly used for debugging and output.

void main() {
  print("Welcome to Dart");
  print(10);
  print(5 + 3);
}

Each print() statement outputs a new line in the console.


Statements and Semicolons

In Dart, every statement must end with a semicolon (;). This tells the compiler that the statement is complete.

void main() {
  print("Statement one");
  print("Statement two");
}

Missing semicolons will result in compilation errors.


Comments in Dart

Comments are used to explain code and make it easier to understand. They are ignored by the compiler.

Single-Line Comment

// This is a single-line comment
print("Hello");

Multi-Line Comment

/*
This is a
multi-line comment
*/
print("Dart Basics");

Good comments improve readability and maintainability.


Running Dart Code

You can run Dart programs using the Dart CLI.

dart run main.dart

This command executes the Dart file and displays the output.


Code Blocks and Execution Flow

Dart executes code line by line from top to bottom inside the main() function.

void main() {
  print("Start");
  print("Processing");
  print("End");
}

The output follows the same order as the code execution.


Whitespace and Formatting

Dart ignores extra spaces and new lines, but proper formatting is strongly recommended. Clean code improves readability and reduces errors.

void main() {
  print("Clean code");
  print("Is easy to read");
}

Common Beginner Mistakes

  • Forgetting semicolons
  • Misspelling main()
  • Using quotes incorrectly
  • Running code outside main()

📝 Practice Exercises

Exercise 1

Write a Dart program that prints your name.

Exercise 2

Print the result of 20 + 30.

Exercise 3

Add a comment explaining what the program does.


✅ Practice Answers

Answer 1

void main() {
  print("My Name");
}

Answer 2

void main() {
  print(20 + 30);
}

What’s Next?

Now that you understand Dart basics, you are ready to learn about variables and how Dart stores data in memory.

In the next lesson, you will explore variables and how to use them effectively.