Programs, Dart and your first program

Dart · Essentials · 8 min

Every later lesson writes and runs Dart code, so it helps to know what happens between typing text and seeing a result.

A program is a list of instructions for a computer, the way a recipe is a list of steps for a cook. The computer follows them exactly, one after another, and never guesses what you meant.

Programmers write those instructions as text in a programming language. Dart is one, made by Google and used to build Flutter apps for phones, websites and desktops, as well as small tools. The text you write is called source code; Dart files end in .dart.

The chip that actually carries out instructions, the processor (or CPU), can't read Dart. It only understands machine code: long lists of numbers. A compiler is a program that translates source code into machine code; that translation is called compiling. For websites, Dart is translated into , the language run, instead.

Before anything runs, Dart checks your code. Mistakes it can spot, such as a typo or using a number where text is expected, are reported as a compile error that points at the exact line. The moment of checking and translating is called compile time; the moment the program actually runs is called runtime. Every mistake caught at compile time is one your users never see.

To get started, install the Dart SDK (software development kit: the compiler plus its helper tools) from dart.dev; it also comes with Flutter. You use it by typing commands in a terminal: a text window where you type a command, press Enter and read the reply.

dart create hello creates a folder with a ready-made project. Inside it, dart run compiles and runs the program in one step. When the program is finished, dart compile exe builds an executable: a file that runs on its own, like any app on your computer.

Every Dart program starts at void main() { ... }. The lines between the curly braces run from top to bottom. print('Hello, world!'); shows a line of text in the terminal. A piece of text between quotation marks is called a string. Each instruction is a statement, and statements end with a semicolon.

Two more commands you will use constantly: dart analyze checks your code for mistakes without running it, and dart format tidies its layout.

Example

// bin/hello.dart: a complete Dart program (this text is source code)
void main() {
  print('Hello, world!');     // Hello, world!
  print('Dart is fun');       // Dart is fun
}

// In a terminal:
//   dart create hello      -> creates a project folder called hello
//   cd hello               -> go into that folder
//   dart run               -> compile and run the program
//   dart analyze           -> check for mistakes without running

// A typo such as  prnt('Hi');  is caught at compile time:
// Error: Method not found: 'prnt'.

Remember

  • Source code is the text you write; the compiler translates it into something the computer runs
  • Dart checks your code at compile time and reports mistakes before the program runs
  • Every program starts at main; print shows a line of text; statements end with ;

Watch out for

  • Skimming past an error message: it names the line and usually says exactly what is wrong.
New words in this lesson (2)
JavaScript
The programming language that web browsers run. Dart code for websites is translated into JavaScript (or WebAssembly).
browser
The app you use to visit websites, like Chrome, Firefox or Safari; it downloads pages and runs their code.

Check yourself

  1. What does a compiler do?
  2. Which command compiles and runs a Dart project in one step?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: dart.dev/tutorials/server/get-started

Builds on

Nothing: this is a foundation.

Unlocks