Chapter 2. A Tour of the Dart Language

This chapter shows you how to use each major Dart feature, from variables and operators to classes and libraries, with the assumption that you already know how to program in another language.

Tip

To play with each feature, create a command-line application project in Dart Editor, as described in Up and Running.

Consult the Dart Language Specification whenever you want more details about a language feature.

A Basic Dart Program

The following code uses many of Dart’s most basic features:

// Define a function.
printNumber(num aNumber) {
  print('The number is $aNumber.'); // Print to the console.
}

// This is where the app starts executing.
main() {
  var number = 42;           // Declare and initialize a variable.
  printNumber(number);       // Call a function.
}

Here’s what this program uses that applies to all (or almost all) Dart apps:

// This is a comment.

Use // to indicate that the rest of the line is a comment. Alternatively, use /* ... */. For details, see Comments.

num

A type. Some of the other built-in types are String, int, and bool.

42

A number literal. Literals are a kind of compile-time constant.

print()

A handy way to display output.

'...' (or "...")

A string literal.

$variableName (or ${expression})

String interpolation, including a variable or expression’s string equivalent inside of a string literal. For more information, see Strings.

main()

The special required top-level function where app execution starts. For more information, see The main() Function.

var

A way to ...

Get Dart: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.