Dart is an object-orientated language with class-based inheritance similar to Java or C#. All types descend from the base Object class. The var keyword declares a variable and is a substitute for a strong type annotation. Variables must be declared before they are used (unlike JavaScript), and objects created by using the new keyword to invoke the object's constructor:
All objects inherit the Object.hashCode() and Object.toString() methods. The toString() method is called automatically whenever you try and convert an object into a string representation, such as when using the print() function:
![Hot Tip](/storage/rc-covers/12975-thumb.png)
Stylistically using the var keyword for variable declaration should be preferred within code bodies. Only use explicit type annotations (such as String, int) at the "surface area" of your code, for example, in function parameter lists, return types, and class properties.
Dart has special support for a number of core, "primitive" types that are baked into the language. Although these types are objects, extending from the Object base class, you create instances of these objects literally, without using the new keyword.
num, int, double
num is the base numeric type, and has two implementations: int and double. num declares the basic operators of +-/* and functions such as abs(), ceil(), floor(), and round(), amongst others.
num
Use num to declare that you want a numeric value, whether an integer or floating-point.
int
The int type accepts integer values of arbitrary precision:
double
The double type accepts 64-bit floating point numbers as specified in the IEEE 754 standard:
bool
In Dart, only the literal boolean true is considered true. All other values are false. An extra restriction in checked mode only allows boolean operations on true and false literal values.
You can us the is and is! syntax to check a type's instance:
String
Dart strings are declared with either single or double quotes, and you can mix strings containing either:
Multi-line strings are declared with three double-quotes:
Strings can also contain escape codes, such as \n for new line:
But you can also declare a raw string that will ignore escape codes by using the prefix r.
String interpolation
Dart strings cannot be concatenated using the + operator. The following code is not valid:
Instead, you must use string interpolation, which converts variables or expressions embedded within a string into a string. The following embeds two variables into a string by prefixing the variable name with $:
An expression enclosed within ${…} is evaluated before it is inserted into the string, for example:
The expression contained within curly braces can be any valid Dart expression, including function calls, for example:
List
Dart doesn't have an explicit array type. Instead, it has a List type, which you can declare using either literal syntax, or as a List object. Internally, the object is the same.
You can add items to these lists by using the add() method, and access values using zero-based indexer syntax [ ]. Use the length property to discover how many items are in the list:
Lists can also be fixed size by using the List.fixedLength() constructor. Lists created in this way cannot have items added to them.
Using Generics
Lists allow use of generic types. This means that you can use type annotations to tell Dart and fellow developers that you are expecting your list to contain only certain types. For example, the following list should contain only Strings. Adding an integer would raise a type warning.
Iterating Lists with a for loop
You can use the standard for loop to iterate a list. The syntax is similar to Java, JavaScript, and C#. This is useful if you want to replace the item at each index:
Iterating Lists with for-in
You can use a for-in construct to access each item in the list:
Iterating Lists with forEach
You can also iterate a list by using a forEach function callback, which calls a function, passing the function each item in the list:
(See the section on Functions for more detailed explanation of anonymous functions).
Other collections
Other collection classes include HashSet, Queue and Set, which offer more specialized functionality
Map
Like Lists, you can create maps (comma-separated list of key:value pairs) using either a map literal or a class constructor:
Using Generics
Any type can be used as a map's key or a value, and you can mix key and value types within a map. To restrict the key or value types, you can create maps using Generic typing (as with Lists)
You access and insert items into a map by using a key as an indexer. If the key does not already exist, it will be replaced.
Accessing a key that has not been added will return null, but you can also explicitly check if the key exists using the containsKey() method.
You can easily access a map's keys and values properties:
You can also iterate the key:value pairs using a forEach function callback: