Dart Syntax

Summary: in this tutorial, you’ll learn about Dart syntaxes including whitespace, statements, identifiers, keywords, literals, and comments.

Introduction to the Dart syntax

The Dart programming language follows the C-style syntax. If you’re familiar with C/C++ or C#, you’ll find similarities in the Dart language.

Whitespace

In Dart, whitespace refers to characters that don’t have visible output, which includes:

  • Carriage return
  • Space
  • New Line
  • Tab

When compiling the source code, the Dart compiler ignores the whitespaces. However, you’ll use whitespaces to format the code to make it easier to read.

For example, the following programs are the same. The first program uses whitespace while the second one does not use newlines. Even though their appearances are different, the Dart compiler will treat both programs the same.

// formatted
void main() {
  bool iOS = true;
  if (iOS) {
    print("iOS");
  }
}Code language: Dart (dart)
// not formtted
void main() { bool iOS = true; if (iOS) { print("iOS");}}Code language: Dart (dart)

Statements

A statement is an instruction that declares a type or instructs the program to perform a task. A statement is always terminated by a semicolon (;). For example, the following program has two statements:

void main() {
  String message = 'Welcome to Dart!';
  print(message);
}Code language: Dart (dart)

The first statement declares a string variable and initializes its value to the string 'Welcome to Dart!':

String message = 'Welcome to Dart!';Code language: JavaScript (javascript)

The second statement displays the value of the message variable to the console:

 print(message);Code language: PHP (php)

Both statements are terminated by the semicolon (;).

Blocks

In Dart, a block is a sequence of zero or more statements. A block is surrounded by curly braces ({}). For example, you can group statements into a block as follows:

{
   String message = 'Welcome to Dart!';
   print(message);
}Code language: Dart (dart)

Unlike a statement, a block is not terminated by a semicolon (;). In practice, you’ll use blocks with the control flow statements like if else, while, do while, and for.

Identifiers

Identifiers are names that you assign to the variables, constants, functions, etc. In Dart, the names of identifiers follow these rules:

  • The alphabetic ([a-z], [A-Z]) and underscore (_) characters can appear at any position.
  • Digits (0-9) cannot be in the first position but everywhere else.

Identifiers are case-sensitive. For example, message and Message identifiers are different.

Keywords

Keywords are names that have a special meaning to the Dart compiler. All keywords are reserved identifiers. Therefore, you cannot use them as the names of identifiers.

The following table shows Dart keywords:

abstractelseimportshow
asenuminstatic
assertexportinterfacesuper
asyncextendsisswitch
awaitextensionlatesync
breakexternallibrarythis
casefactorymixinthrow
catchfalsenewtrue
classfinalnulltry
constfinallyontypedef
continueforoperatorvar
covariantFunctionpartvoid
defaultgetrequiredwhile
deferredhiderethrowwith
doifreturnyield
dynamicimplementsset 

Literals

Literals are primitive values in the program. For example, an integer has the following literal:

10Code language: Dart (dart)

To form a literal string, you surround text with single quotes ('), double quotes ("), or triple quotes ("""). For example:

'Welcome to Dart!'Code language: Dart (dart)

Comments

Comments help you to document your code. Dart has the following types of comments:

  • Single-line comments
  • Block comments
  • Doc comments

The Dart compiler ignores the comments when compiling the code.

Single-line comments

A single-line comment starts with a double forward slash (//) and continues to the end of the line. For example:

String message = 'Welcome to Dart!'; // a greeting messageCode language: Dart (dart)

In this example, The following is a single-line comment:

// a greeting messageCode language: Dart (dart)

Block comments

A block comment starts with /* and ends with */. A block comment can span any number of lines. For example:

/*
    A block comment can span 
    multiple lines
*/Code language: Dart (dart)

Doc comments

A Doc comment starts with the /// and appears before any declaration. It is used by the dart doc command line to generate beautiful documentation. For example:

/// The greeting message
String message = 'Welcome to Dart!';Code language: JavaScript (javascript)

Summary

  • Whitespaces are carriage return, space, newline, and tab which are ignored by the Dart compiler.
  • Statements are terminated with a semicolon (;).
  • Blocks consist of one or statements surrounded by curly braces ({}).
  • Identifiers are case-sensitive.
  • Do use comments to document the code and explain why the code does something.
Was this tutorial helpful ?