Dart from the ground up

Your first Dart program

What Dart runs on, the main function every program begins at, print and the line breaks it adds, and the comments that are for the reader rather than for the compiler.

Part 1 of 12

Beginner14 min readBack to Courses

A language for phones, browsers and servers

Dart began as a language for the browser and grew into the language behind Flutter, the toolkit for phone and desktop apps. It is comfortable on a server too. The same file you write here can run in all of those places, which is the reason to learn the language once.

The editor on this page sends your program to a real Dart compiler and shows what it printed. Nothing here supplies input, so every example writes its own values and prints its own answers. That keeps each lesson predictable, and it is the only limitation this course asks you to accept.

Every program starts at main

A Dart file runs from a function called main. The word void says the function hands nothing back, main is its name, the empty parentheses hold its inputs — none so far — and the braces hold the lines that run.

Each statement ends with a semicolon. Dart does not care how you indent, but your reader does: four spaces per level is the habit this course uses, and the compiler only needs the braces.

Printing a line, and the comment

print shows one value and moves to the next line, so one call gives one line of output. The value can be text in double quotes, a number, or an arithmetic expression that is worked out before it is printed. Write print("") and you get an empty line, which is a useful way to space out a report.

Two slashes start a comment that runs to the end of the line. The compiler reads it and throws it away, so a comment is for the next person reading the file — often you, a week later. The double quotes matter: they are what tells Dart that these characters are text and not names from your program.

What the compiler does before the first line runs

Pressing Run is two steps, not one. A compiler reads the whole file first and turns it into a program, and only then does that program start at main. A mistake anywhere in the file stops the first step, so a program with an error on its last line prints nothing at all — not even the lines above it.

That is why the output pane can show a message from the compiler instead of text from your program. The compiler reports the first problem it cannot get past, with the line number where it found it. Fix that line and run again; the next message, if there is one, was waiting behind it.

The same rule explains why a typo in a name is caught before the program runs. Dart checks that every name exists and that every value has the type its neighbours expect. When those checks pass, the program is already known to be free of that whole family of mistakes.

Lines run in order, and braces decide what runs

Inside main, statements run from top to bottom, one after another. The order of the output is the order of the print calls, which is why moving a line moves its line of output.

Braces group lines into a block. A block belongs to the statement in front of it, and its lines run only when that statement says so. So far every block is the body of main, but the same braces will later hold the body of an if or a loop, and the indentation is there to show you which block a line belongs to.

Whitespace itself means nothing to the compiler. You could put the whole program on one line and it would run the same. Keep the layout anyway: the compiler reads braces, and people read shape.

Four prints, one file

void main() {
  print("One line");
  print("");          // an empty line
  print(6 * 7);       // the value is worked out first
  // Two slashes start a comment.
  print("Done");
}

Run it, then make it yours

Press Run and read the output one line at a time. Then print a sum of your own, add a second name, and move one of the last two lines above the other so you can watch the order follow.

Output
Click "Run" to execute your code

Read the output from the top and count the lines. A missing quote turns the rest of the file into text and the compiler complains about that line, not about the line you meant to fix.

Three mistakes account for most first runs that print nothing. A missing semicolon at the end of a statement; a quote opened and never closed, which turns the rest of the file into text; and a name printed without quotes, which the compiler reads as a variable that does not exist. Read the first message, fix that line, and run again.

Try these next

  • Print your name, then an empty line, then a line with a sum, and count the three lines in the output.
  • Put a comment above each print and confirm that the output does not change.
  • Remove a semicolon on purpose, run the program, and read the first compiler message before you put it back.

A packing slip, printed line by line

Run it and read the slip from the first line to the last. Then change the item and the quantity, and add a line that prints the unit price in cents beside the price in dollars.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Which function does a Dart program start at?