Skip to main content
Get up and running with Dart quickly by writing and running your first program.

Prerequisites

Before you begin, install the Dart SDK on your system.

Your first Dart program

1

Create a Dart file

Create a new file called hello.dart:
hello.dart
void main() {
  print('Hello, World!');
}
2

Run the program

Execute your Dart program using the dart command:
dart hello.dart
You’ll see the output:
Hello, World!

Working with arguments

Dart makes it easy to work with command-line arguments:
greet.dart
void main(List<String> arguments) {
  if (arguments.isEmpty) {
    print('Usage: dart greet.dart <name>');
    return;
  }
  
  final name = arguments.first;
  print('Hello, $name!');
}
Run it with:
dart greet.dart Alice
# Output: Hello, Alice!

Language basics

Variables and types

Dart is strongly typed with type inference and null safety:
void main() {
  // Type inference
  var name = 'Alice';
  var age = 30;
  
  // Explicit types
  String city = 'New York';
  int? nullableAge;  // Can be null
  
  // Null safety
  print(nullableAge ?? 0);  // Use 0 if null
}

Collections

Work with lists, sets, and maps:
void main() {
  // Lists
  var numbers = [1, 2, 3, 4, 5];
  numbers.add(6);
  
  // Sets
  var uniqueNames = {'Alice', 'Bob', 'Alice'};  // Only 2 items
  
  // Maps
  var person = {
    'name': 'Alice',
    'age': 30,
    'city': 'New York',
  };
  
  print(person['name']);  // Alice
}

Functions

Dart supports multiple function styles:
// Regular function
int add(int a, int b) {
  return a + b;
}

// Arrow function
int multiply(int a, int b) => a * b;

// Named parameters
void greet({required String name, String greeting = 'Hello'}) {
  print('$greeting, $name!');
}

void main() {
  print(add(2, 3));        // 5
  print(multiply(4, 5));   // 20
  greet(name: 'Alice');    // Hello, Alice!
}

Control flow

Use modern control flow with pattern matching:
void main() {
  var score = 85;
  
  // if-else
  if (score >= 90) {
    print('A');
  } else if (score >= 80) {
    print('B');
  } else {
    print('C');
  }
  
  // Switch expressions
  var grade = switch (score) {
    >= 90 => 'A',
    >= 80 => 'B',
    >= 70 => 'C',
    _ => 'F'
  };
  print(grade);  // B
}

Asynchronous programming

Dart has first-class support for async/await:
import 'dart:async';

Future<String> fetchUserData() async {
  // Simulate network delay
  await Future.delayed(Duration(seconds: 1));
  return 'User data loaded';
}

void main() async {
  print('Fetching user data...');
  var data = await fetchUserData();
  print(data);  // User data loaded (after 1 second)
}

Working with Streams

Handle multiple asynchronous values:
Stream<int> countStream(int max) async* {
  for (int i = 1; i <= max; i++) {
    await Future.delayed(Duration(milliseconds: 100));
    yield i;
  }
}

void main() async {
  await for (var number in countStream(5)) {
    print(number);  // Prints 1, 2, 3, 4, 5 with delays
  }
}

Creating a project

Use dart create to set up a new project:
dart create -t console my_app
cd my_app
dart run

Compiling to native code

Compile your Dart code to a standalone executable:
dart compile exe hello.dart -o hello
./hello  # Run the native binary
Native executables start instantly and don’t require the Dart SDK to run.

Next steps

Core Libraries

Explore dart:core, dart:async, dart:io and more

Language Tour

Learn all Dart language features in depth

Compilers

Understand JIT, AOT, dart2js, and dart2wasm

Tools & CLI

Master the dart command and SDK tools

Useful commands

dart analyze

Static analysis of your code

dart format

Format code to style guide

dart test

Run your unit tests

dart pub

Manage package dependencies

Build docs developers (and LLMs) love