Dart Tutorial
🇺🇸

Dart Beginner Lesson 3 of 115 2 min read

Your First Dart Program: Hello World Explained

Write Hello World in Dart and understand every part of it: the main function, print, statements, semicolons and how to run the file.

On this page

Every Dart program, from a five-line script to a full mobile app, starts running at the same place: a function called main.

Hello, world

Create a file named hello.dart and type this in:

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

Run it from the terminal with dart run hello.dart, or press Run above.

Hello, world!

Reading the program piece by piece

PartMeaning
voidThe function gives nothing back when it finishes
mainThe special name Dart looks for to start the program
()The function takes no input here
{ ... }The body: the statements that run, top to bottom
print(...)A built-in function that writes a line to the console
'Hello, world!'A string, which is a piece of text
;Ends a statement. Dart requires it

Statements run in order

void main() {
  print('Step 1: boil water');
  print('Step 2: add tea');
  print('Step 3: wait three minutes');
}
Step 1: boil water
Step 2: add tea
Step 3: wait three minutes

Printing values, not just text

print accepts any value. Put a $ in front of a variable name inside a string to drop its value in. Use ${...} for a calculation.

void main() {
  var name = 'Maya';
  var age = 21;

  print(name);
  print(age);
  print('$name is $age years old.');
  print('Next year she will be ${age + 1}.');
}
Maya
21
Maya is 21 years old.
Next year she will be 22.

A shorter main

When a function body is a single expression, you can write it with an arrow.

void main() => print('Hello, world!');

Reading command-line arguments

main can receive the words typed after the file name.

void main(List<String> args) {
  print('You passed ${args.length} argument(s): $args');
}

Running dart run hello.dart red blue prints You passed 2 argument(s): [red, blue].

Common mistakes

  • Forgetting the semicolon. print('hi') without ; is an error.
  • Wrong capital letters. Dart is case sensitive. Print and Main do not exist.
  • Mismatched quotes. A string that starts with ' must end with '.
  • Code outside a function. Statements such as print(...) must be inside main or another function.

Try it yourself

Write a program that prints your name, your city and your favorite food on three separate lines. Then change it so all three appear in a single sentence using $ interpolation.

Practise in the playground Updated

Stuck? Look up your error message, check a term in the glossary, or send us your question.