On this page10 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The Smallest Program
  4. 2. Every Part Explained
  5. 3. Top to Bottom
  6. Code Examples
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 004 — Your First Program: Hello World

Category2 — Getting Started
Lecture004 of 057
Estimated video length10 min
TargetDart 3.12
LevelBeginner

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, (), {}, print and ;
  • Print several lines

1. The Smallest Program

In DartPad, or in bin/main.dart in your project, type:

dart
void main() {
  print('Hello, World!');
}

Run it (▶ in IntelliJ, Run in DartPad, or dart run bin/main.dart):

code
Hello, World!

2. Every Part Explained

PieceMeaning
mainthe function Dart calls first — every program starts here
voidmain 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:

dart
void main() {
  print('First');
  print('Second');
  print('Third');
}
code
First
Second
Third

Code Examples

Example 1 — Introduce Yourself

dart
void main() {
  print('Hello, World!');
  print('My name is Hamza.');
  print('I am learning Dart.');
  print("Today is Day 2.");
}

Output

code
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

  1. Which function does every Dart program start from?
  2. What does void mean before main?
  3. What does ; do?
  4. Write a program that prints your name and your city on two lines.

Practice Answers

  1. main.
  2. main returns no value.
  3. Ends an instruction.
  4. void main() { print('Ali'); print('Karachi'); }

Key Takeaways

  • A Dart program starts at void main() { ... }.
  • print writes one line of text.
  • Instructions run top to bottom and end with ;.
  • Dart is case-sensitive: print, not Print.