On this page10 sections
Lecture 004 — Your First Program: Hello World
| Category | 2 — Getting Started |
| Lecture | 004 of 057 |
| Estimated video length | 10 min |
| Target | Dart 3.12 |
| Level | Beginner |
We write a program from an empty file and explain every symbol in it.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lecture 003: DartPad open, or a project in IntelliJ.
Learning Objectives
- Write and run a program from scratch
- Explain
void,main,(),{},printand; - Print several lines
1. The Smallest Program
In DartPad, or in bin/main.dart in your project, type:
void main() {
print('Hello, World!');
}Run it (▶ in IntelliJ, Run in DartPad, or dart run bin/main.dart):
Hello, World!2. Every Part Explained
| Piece | Meaning |
|---|---|
main | the function Dart calls first — every program starts here |
void | main gives nothing back when it finishes |
() | the list of inputs; empty here |
{ } | the body — the instructions, run top to bottom |
print(...) | writes text to the output, then a new line |
'Hello, World!' | a string — text inside quotes (single or double) |
; | ends an instruction, like a full stop |
3. Top to Bottom
Instructions inside main run in order, one after another:
void main() {
print('First');
print('Second');
print('Third');
}First
Second
ThirdCode Examples
Example 1 — Introduce Yourself
void main() {
print('Hello, World!');
print('My name is Hamza.');
print('I am learning Dart.');
print("Today is Day 2.");
}Output
Hello, World!
My name is Hamza.
I am learning Dart.
Today is Day 2.Single '...' and double "..." quotes both make strings. Pick one style and stick to it —
Dart's own style uses single quotes.
Common Beginner Mistakes
1. Forgetting the ; — print('Hi') alone gives Expected ';' after this. Every
instruction ends with a semicolon.
2. Mismatched quotes — print('Hi"); starts with ' and ends with ". Use the same kind
on both sides.
3. Capital letters — Print or Main are different names from print and main. Dart is
case-sensitive.
4. Code outside main — a print(...) line outside the { } of main is an error.
Practice Questions
- Which function does every Dart program start from?
- What does
voidmean beforemain? - What does
;do? - Write a program that prints your name and your city on two lines.
Practice Answers
main.mainreturns no value.- Ends an instruction.
void main() { print('Ali'); print('Karachi'); }
Key Takeaways
- A Dart program starts at
void main() { ... }. printwrites one line of text.- Instructions run top to bottom and end with
;. - Dart is case-sensitive:
print, notPrint.