A basic Dart program

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

  1. // Define a function.
  2. printInteger(int aNumber) {
  3. print('The number is $aNumber.'); // Print to console.
  4. }
  5. // This is where the app starts executing.
  6. main() {
  7. var number = 42; // Declare and initialize a variable.
  8. printInteger(number); // Call a function.
  9. }

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

  • // This is a comment.
  • A single-line comment.Dart also supports multi-line and document comments.For details, see Comments.
  • int
  • A type. Some of the other built-in typesare String, List, and bool.
  • 42
  • A number literal. Number 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 stringequivalent inside of a string literal. For more information, seeStrings.
  • main()
  • The special, required, top-level function where app executionstarts. For more information, seeThe main() function.
  • var
  • A way to declare a variable without specifying its type.

Note: This site’s code follows the conventions in the Dart style guide.