Sage-Code Laboratory
index<--

Dart Syntax

Dart is a free form, curly bracket language from C family. It has classic features like comments, code blocks and control statements very similar to Kotlin, Swift, Go and Rust. Don't warry if you do not know these languages. We will explain everything in this article.

Let's start with an example. Observe the syntax and then read the notes below to understand what you see. This is how we do it in this tutorial. We give some examples then we explain the syntax.

Example:

Next example is a demo program. It has several variables and a method.

/* simple dart program */
void main() {
  print('Hello, World!'); //output
}

Rules:

Program Elements

A Dart program like any other computer program consist of code, libraries and data files. For now let's focus our attention of code. What are the elements we must understend to be able to read and write Dart code.

Comments

Let's start with documenting your code. When you write new code, you should add descriptions and ideas about your thinking and what your code will do. You can write this documentation inside your code by using "block comments". This is a short text surrounded by symbols /* ... */. It can span several lines of text.

Anoder more simple style to write comments is called "single line" comments. This kind of comments start with double slash symbol: // (like in C). You can add line comments at end of statement but not in the middle of a statement.

Finally, "expression comments" are comments that you can write insite of an expression or in the middle of a statement. Unfortunately Dart is using same notation for this kind of comments as block comments: /*...*/. This we know can make code unreadable.

Variables

Dart is a strongly typed language. Unlike Java in Dart you do not have to specify type all the time. The compiler is smart enaugh to assign a default type for your variables. This technology is called "type inference" and is common in modern languages.

/* example of variables */
var name = 'Voyager I'; //string variable
var year = 1977;        //integer variable
var antennaDiameter = 3.7; //float variable

/* list collection */
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];

/* composite variable (JSON) */
var image = {
  'tags': ['saturn'],
  'url': '//path/to/saturn.jpg'
};

/* useing type annotation */
String name = "Bob";

/* uninitialized numbers are null */
int LineCount; 
assert(lineCount == null);

Note:In Dart, all things are Objects. Even the primitive types are objects. Every object is an instance of a class. Even functions, and null values are objects. All objects inherit from the Object class.

Rules:

1. You declare a variable using keyword: "var" follow by an "identifier". That is a name we give to a variable. The name is case sensitive and usually starts with lowercase character and can contain numbers, uppercase characters and underscore

2. Unlike Java in Dart we do not use "accesor modifiers". In Dart all variables are private into a library or scope, except variables that start with underscore. These variables are public.

3. Uninitialized variables have an initial value of null. Even variables with numeric types are initially null, because numbers, like everything else in Dart are objects.

Expressions

This concept should be familiar to you from mathematics. We form an expression in Dart using identifiers, functions, operators and pubctuation symbols like round paranthesis, "()" and comma ",". Simplest expressions are constant literals.

Expressions return a value that can be captured into a variable or it can be used immediatly to create larger expressions. Expression value is computed by algorithms associated to each operator. We can use round paranthesis in larger expressions to alter the order of evaluation for each smaller expressions.

/* simple expressions */
1    //a constant literal
3/2; //a division
3*2; //a multiplication

/* two complex expressions */
(a + b + c)/3 
sqrt(pow(width,2) + pow(height,2))

Statements

Statements are specific to programming but one particular statement is most common and also known in mathematics. This is called assignment and is done in Dart using symbol equal "=". Other statements in Dart consist of a logical enumeration of keywords, expressions and punctuation symbols.

One statement can be usually a single line of code terminated with semicolumn ";". Other times we can have more then one statement in a single line of code. Each statement is separated by semicolumn ";" from the next one.

There are statements that can span several lines of code. Some statements have one or more blocks of code that have inside other statements. This is how we define a "scope" for a statement.

Code Blocks

A block of code is a group of statements enclosed in squigly brackets: { ... }. We use blocks of code to define a section of code. When we declare a variable in a section, the variable become local and is visible only in current scope. This is good to encapsulate the logick and avoid loosing track of purpose for a specific variable or identifier.

Keywords

Dart use English short words and abbreviations that are part of Dart syntax and can not be used for any other purpose in a program, except in comments. These are also called "reserved words" and you must avoid using these words as identifiers when you create code. Dart has 61 keywords:

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

Dart Operators

Dart supports operators symilar to all C based languages. The operators are polymorphic that means operators can operate on multiple data types. Each operator is in fact a method that supports two parameters (operands) and produce one result. Some operators need only one operand.

We have listed operators to be learned in logical order. So that you understand better we start with most simple operators and continue down with more complex.

Operator type Description
Arithmetic OperatorsCan operate on numbers, return numbers
Bitwise Operators Can operate on integer numbers, return integer.
Logical Operators Can operate on Booleans, return Boolean
Comparison OperatorsCan operate on numbers, return Boolean
Assignment OperatorsCan operate on a variable and expression of same type

Arithmetic Operators

These operators are used to create numeric expressions.

Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
~/ Division Integer division x ~/ 2
% Modulus Returns the division remainder x % y
++ Increment Equivalent to x = x + 1 ++x or x++
-- Decrement Equivalent to x = x - 1 --x or x--

Bitwise Operators

Given two binary numbers A = 0011 and B=1010 here are the operators and results:

Operator Meaning Example Result
& bit and A & B 0010
| bit or A | B 1011
^ bit xor A ^ B 1001
~ bit not ~A 1100
<< shift left 2 << A1100
>> shift rightB >> 2 0010

Logical Operators

These operators are also known as Boolean operators. They accept arguments of type Boolean and return Boolean results: true or false. Notice in Dart true is greater then false. Let's consider a == true and b == true in next examples:

Operator Name Description Expression Result
&&  and Returns true if both statements are true a && b true
||  or Returns true if one of the statements is true a || b true
! not Reverse the result, returns false if the result is true !a false
? if Ternary operator a ? 0 : 1 0

Comparison Operators

These operators are used to in logical expressions but they compare any kind of things. In fact they are polimorphic operators. Can compare number primarly but also boolean or string operands. Important fact to know is that alwais return true/false Boolean values.

Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Assignment Operators

These operators are also called "in-place" operators or "variable modifiers". Have double role: make an arithmetic operation and assign the result to left operand. The right operand can be a constant, variable or expression that return same tipe as the left operand.

Operator Name Expression Equivalent
= Assignment x = 1 x = 1
+= Addition x += 1 x = x + 1
-= Subtraction x -= 1 x = x - 1
*= Multiplication x *= 1 x = x * 1
/= Division x /= 1 x = x / 1
%= Modulo x %= 1 x = x % 1
&= Bitweese and x &= 1 x = x & 1
|= Bitweese or x |= 1 x = x | 1
^= Bitweese not x ^= 1 x = x ^ 1
>>= left shift x >>= 1 x = x >> 1
<<= right shift x <<= 1 x = x << 1

Special Operators

These operators are may produce different kind of data types depending on the expressions or statements they are used with. We have similar operators in other languages.

Operator Name Description
() call Call a function or method
[] index access Access members into a list
. member accessIdentify a member or field
?. cond. accessUsed for nullable members
?? if null Conditional expressions
.. cascade Sequences of operations

Keyword Operators

Next operators are based on keywords.

Operator Meaning
as Typecast (also used to specify library prefixes)
is True if the object has the specified type
is! False if the object has the specified type

Read next: Data Types