Beginner

Hello World in Dart

Your first Dart program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Dart program.

The Code

Create a file named hello.dart:

1
2
3
void main() {
  print('Hello, World!');
}

Clean and simple! Let’s understand what’s happening.

Understanding the Code

  • void main() - The entry point of every Dart program. void means the function returns nothing.
  • print() - Outputs text to the console with a newline.
  • 'Hello, World!' - A string literal. Dart supports both single and double quotes.
  • Semicolons - Required at the end of statements (like Java, C#, JavaScript).
  • Curly braces - Define code blocks.

Running with Docker

The easiest way to run Dart without installing it locally:

1
2
3
4
5
# Pull the official Dart image
docker pull dart:stable

# Run the program
docker run --rm -v $(pwd):/app -w /app dart:stable dart run hello.dart

Running Locally

If you have the Dart SDK installed:

1
2
3
4
5
6
# Run directly (recommended)
dart run hello.dart

# Or compile to a native executable
dart compile exe hello.dart -o hello
./hello

Expected Output

Hello, World!

Key Concepts

  1. main() is required - Every Dart program must have a main() function
  2. Strong typing - Dart is statically typed, but types can be inferred
  3. Modern syntax - Familiar to Java, C#, and JavaScript developers
  4. Fast execution - Dart compiles to efficient native code or JavaScript

Adding Types

Dart has excellent type inference, but you can be explicit:

1
2
3
4
void main() {
  String message = 'Hello, World!';
  print(message);
}

The String type annotation is optional here - Dart infers it from the assignment.

A More Dart-Like Version

Let’s use some Dart-specific features:

1
2
3
4
5
6
7
void main() {
  var greeting = 'Hello';  // Type inference - Dart knows this is a String
  var name = 'World';

  // String interpolation with $
  print('$greeting, $name!');
}

String interpolation with $ is cleaner than concatenation:

  • $variable for simple variables
  • ${expression} for complex expressions

Functions in Dart

Dart has concise function syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Traditional function
String greet(String name) {
  return 'Hello, $name!';
}

// Arrow function (for single expressions)
String greetArrow(String name) => 'Hello, $name!';

void main() {
  print(greet('World'));
  print(greetArrow('Dart'));
}

Null Safety Example

Dart’s null safety prevents null reference errors:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
void main() {
  String? nullableName;  // Can be null (note the ?)
  String nonNullableName = 'World';  // Cannot be null

  // This would be a compile error:
  // print(nullableName.length);  // Error: nullableName might be null

  // Safe access with null check
  print(nullableName?.length ?? 0);  // Prints 0
  print(nonNullableName.length);     // Prints 5

  print('Hello, $nonNullableName!');
}

A Complete Example with Classes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Greeter {
  final String greeting;

  Greeter(this.greeting);  // Concise constructor syntax

  String greet(String name) => '$greeting, $name!';
}

void main() {
  var greeter = Greeter('Hello');
  print(greeter.greet('World'));
}

This demonstrates:

  • Classes - Dart is object-oriented
  • final - Immutable instance variables
  • Constructor shorthand - this.greeting automatically assigns the parameter
  • Arrow functions - Concise single-expression methods

Dart vs. Other Languages

Java:

1
2
3
4
5
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

JavaScript:

1
console.log("Hello, World!");

Dart:

1
2
3
void main() {
  print('Hello, World!');
}

Dart strikes a balance - more structured than JavaScript, less verbose than Java.

Next Steps

Continue to Variables and Types to explore Dart’s type system and null safety in depth.

Running Today

All examples can be run using Docker:

docker pull dart:stable
Last updated: