Hello World in Dart

Our first program will print the classic “hello world” message. Here’s the full source code.

void main() {
  print('hello world');
}

To run the program, put the code in a Dart file (e.g., hello_world.dart) and use the Dart SDK to run it.

$ dart run hello_world.dart
hello world

Sometimes we’ll want to build our programs into binaries or executables. In Dart, we can compile our scripts into native executables using dart compile exe.

First, compile the Dart code into an executable:

$ dart compile exe hello_world.dart -o hello_world
$ ls
hello_world  hello_world.dart

We can then execute the built binary directly.

$ ./hello_world
hello world

Now that we can run and build basic Dart programs, let’s learn more about the language.