[Dart Language Tour] A basic Dart program, Important concepts, Keywords

A basic Dart program, Important concepts, Keywords

This page 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. For a briefer, less complete introduction to the language, see the language samples page - https://dart.dev/samples.

To learn more about Dart’s core libraries, see the library tour - https://dart.dev/guides/libraries/library-tour. Whenever you want more details about a language feature, consult the Dart language specification - https://dart.dev/guides/language/spec.

Note: You can play with most of Dart’s language features using DartPad (learn more - https://dart.dev/tools/dartpad). Open DartPad - https://dartpad.dev/.

This page uses embedded DartPads to display some of the examples. If you see empty boxes instead of DartPads, go to the DartPad troubleshooting page - https://dart.dev/tools/dartpad/troubleshoot.

A basic Dart program

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

// Define a function.
void printInteger(int aNumber) {
print('The number is $aNumber.'); // Print to console.

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

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

  • // This is a comment.

    A single-line comment. Dart also supports multi-line and document comments. For details, see Comments.

  • void

    A special type that indicates a value that’s never used. Functions like printInteger() and main() that don’t explicitly return a value have the void return type. For more information, see this article.

  • int

    Another type, indicating an integer. Some additional built-in types are 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 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 declare a variable without specifying its type.

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

Important concepts

As you learn about the Dart language, keep these facts and concepts in mind:

  • Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects. All objects inherit from the Object class.

  • Although Dart is strongly typed, type annotations are optional because Dart can infer types. In the code above, number is inferred to be of type int. When you want to explicitly say that no type is expected, use the special type dynamic.

  • Dart supports generic types, like List<int> (a list of integers) or List<dynamic> (a list of objects of any type).

  • Dart supports top-level functions (such as main()), as well as functions tied to a class or object (static and instance methods, respectively). You can also create functions within functions (nested or local functions).

  • Similarly, Dart supports top-level variables, as well as variables tied to a class or object (static and instance variables). Instance variables are sometimes known as fields or properties.

  • Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore (_), it’s private to its library. For details, see Libraries and visibility.

  • Identifiers can start with a letter or underscore (_), followed by any combination of those characters plus digits.

  • Dart has both expressions (which have runtime values) and statements (which don’t). For example, the conditional expression condition ? expr1 : expr2 has a value of expr1 or expr2. Compare that to an if-else statement, which has no value. A statement often contains one or more expressions, but an expression can’t directly contain a statement.

  • Dart tools can report two kinds of problems: warnings and errors. Warnings are just indications that your code might not work, but they don’t prevent your program from executing. Errors can be either compile-time or run-time. A compile-time error prevents the code from executing at all; a run-time error results in an exception being raised while the code executes.

Note: If you’re curious why Dart uses underscores instead of access modifier keywords like public or private, see SDK issue 33383.


The following table lists the words that the Dart language treats specially.

abstract 2	else	import 2	super
as 2 enum in switch
assert export 2 interface 2 sync 1
async 1 extends is this
await 3 extension 2 library 2 throw
break external 2 mixin 2 true
case factory 2 new try
catch false null typedef 2
class final on 1 var
const finally operator 2 void
continue for part 2 while
covariant 2 Function 2 rethrow with
default get 2 return yield 3
deferred 2 hide 1 set 2
do if show 1
dynamic 2 implements 2 static 2

Avoid using these words as identifiers. However, if necessary, the keywords marked with superscripts can be identifiers:

Words with the superscript 1 are contextual keywords, which have meaning only in specific places. They’re valid identifiers everywhere.

Words with the superscript 2 are built-in identifiers. To simplify the task of porting JavaScript code to Dart, these keywords are valid identifiers in most places, but they can’t be used as class or type names, or as import prefixes.

Words with the superscript 3 are limited reserved words related to asynchrony support. You can’t use await or yield as an identifier in any function body marked with async, async*, or sync*.

All other words in the table are reserved words, which can’t be identifiers.


[1] Language tour | Dart - https://dart.dev/guides/language/language-tour

[2] Effective Dart: Design | Dart - https://dart.dev/guides/language/effective-dart/design