Dart uses literals for some types like string or collections to provide an easy and intuitive use of these objects. We will learn how to use the literals.

Introduction

So what are literals? Literals are used to initialize new objects of certain types. Literals only exists for a few selected types. The most common example is the String type. 'Hello world' creates a string without calling a constructor or anything. This is called a string literal.

String literals

String literals exists in almost every programming language. An easy example is:

var str  =  'Hello world';

It is also possible to use double quotes:

var str = "Hello world";

And thats it, there must not be called a constructor or anything. A text in quotes is enough to create a String. You can even do this:

var str = 'hello' 'world'; 

Which is the same as 'hello' + 'world' and will create the string 'helloworld'.

String literals in Dart are more powerful than string literals in other languages. It is possible to access variable values inside literals or even do more complex operations which return an object/string.

The following example will print ‘Hello my name is Peter‘:

var name = 'Peter';
print('Hello my name is $name');

If the value you want to use is more complex than a simple variable you need to surround the code by curly brackets. For example:

String getName() => 'Peter';
print('Hello my name is ${getName()}');

You write whatever code you want inside the curly brackets as long as it returns a string or an object because the toString() method is called implicitly. So this would also be possible:

var x = 5; 
var y = 7;
print('$x + $y is equal to ${x + y}');

Collection literals

Dart has also literals for collections. This allows us to create Sets, lists and maps with literals.

List literal

A list can be created using []:

var list = [];

This will create an empty list. A list normally has a generic type parameter. We can set the generic type by writing it before the literal:

var list = [];

This will create an empty list from type List<int>.

We also can pass values to the literal to initialize a non empty list:

var list = [1,2,3];

This will create a list from type List<int> with the values 1, 2 and 3. Note that the generic type is omitted here because the generic type is implicitly taken from the passed values.

This can lead to unexpected behavior if you want to create a List<num> and initialize it as:

var list = [1,2,3];
list.add(2.5); // This is not working

To make it work you need to pass the generic type to the literal:

var list = [1,2,3];
list.add(2.5); // This is working
Map literal

A map can be created by using curly brackets:

var map = {};

This will create a Map<dynamic, dynamic>. To define the generic parameters they can be again added before the literal:

var map = {};

Values can also be added passed to literal:

var map = {'Peter' : 1, 'Bob': 45};

The generic type parameter can be omitted because it is interfered from the passed values.

Set literal

The set literal is the same as the map literal. The following statement creates a set:

var mySet = {};

This will create a Set<int>. Note that the generic parameter can’t be omitted because otherwise a map would be created because both data structures are using the same literal. Because in this example only one generic parameter is passed the Dart compiler knows that it must be a set because a map requires two parameters. The other option is to annotate the variable with the type Set mySet = {} but the type annotation on the literal should be preferred.

Again values can be passed to the literal:

var mySet = {1,2,3,4};

This will create a Set<int> with the values 1,2,3 and 4. Here the generic type can be omitted because the compiler knows the type because of the passed values.

For a comparison between list and set take a look on the article about the differences between set and lists,