Your First Dart Program
Every programming journey begins with "Hello, World!" — a simple program that displays a message on the screen. In Dart it looks like this:
void main() {
print('Hello, World!');
}
Anatomy of the Program
| Part | Meaning |
void | The return type of the function — void means the function returns nothing. |
main() | The entry point of every Dart program. Execution starts here. |
{ } | Curly braces define the body (block) of the function. |
print() | A built-in function that outputs text to the console. |
'Hello, World!' | A string literal — text enclosed in single or double quotes. |
; | Semicolon marks the end of a statement in Dart. |
How to Run
Option 1 — DartPad: Paste the code into dartpad.dev and click Run.
Option 2 — Terminal:
# Save as hello.dart, then:
dart run hello.dart
Option 3 — VS Code: Open the project folder, then press F5.
Variations
You can print multiple lines and use string interpolation:
void main() {
print('Hello, World!');
print('Welcome to Dart Programming!');
String name = 'Alice';
int year = 2024;
print('Hello, $name! It is $year.');
}
Output:
Hello, World!
Welcome to Dart Programming!
Hello, Alice! It is 2024.
⚠️ Common Mistakes
- Missing semicolon:
print('Hello') without ; causes a syntax error.
- Wrong function name: The entry point must be exactly
main(), not Main() or MAIN().
- Missing quotes:
print(Hello) tries to find a variable named Hello — wrap text in quotes.
- Mismatched braces: Every
{ must have a matching }.
💪 Exercise
- Write a Dart program that prints your full name.
- Print your name, city, and favourite food on separate lines.
- Use string interpolation to print: "My name is [name] and I am [age] years old."
🧠 Quiz
1. What is the entry point of a Dart program?
- A) start()
- B) run()
- C) main() ✅
- D) begin()
2. Which function prints output to the console in Dart?
- A) console.log()
- B) System.out.println()
- C) print() ✅
- D) echo()
3. What does void mean in void main()?
- A) The function is empty
- B) The function returns nothing ✅
- C) The function is optional
- D) The function is private
Summary
Every Dart program starts with the main() function. Use print() to display output. Statements end with a semicolon. You can run Dart programs using DartPad, the terminal (dart run), or VS Code (F5).