Home Tutorials Training Consulting Products Books Company Donate Contact us

Get more...

Training Events

This tutorial gives a basic overview over the programming language Dart

1. Dart

The Dart programming language is a language developed by Google. It is widely used at Google and it is used for:

  • mobile app development using Flutter

  • web development (using Dart2JS)

  • server side (using various libraries and frameworks)

The syntax of Dart is very close to Java and C++. Therefore, the object oriented language is very approachable for developers with knowledge in other programming languages.

2. Installation

To be able to run or compile Dart apps you will need to have the Dart SDK installed. The SDK can be downloaded from the Dart installation website.

Follow the instructions for your operating system.

After that the Dart SDK should be automatically added to your path.

To test this type dart --version in a terminal.


On Linux Dart is typically installed in /usr/lib/dart/bin. To add it to your path, simply add export $PATH:/usr/lib/dart/bin at the end of the .bashrc file in your home directory.


Add /Applications/dart/dart-sdk/bin at the end of /etc/paths.

2.1. Using additional modules

Dart comes with some other modules that may need to be added to the path.

One of them is the Dart Package Manager "Pub". Having it on the path can be handy as it allows you to use packages from the pub registry, similar to NPM.

To make all of the extra tools available anywhere, add the folder <dart-location>/bin to your path.

On most Linux distributions and macOS you can find the <dart-location> by typing which dart into a terminal.

3. Development

Dart execution can be triggered from the command line. To develop and run a Dart application, you therefore only need the Dart SDK and a text editor.

However it is recommended to use an IDE as it can help with more complex tasks. Such tools are:

3.1. Running a Dart application

Running a Dart program requires a file with a main() method. This file can then be executed via the following command line:

`$ dart path-to-file.dart`.

The compiler will automatically pick up the main() method and execute it.

3.2. Packaging / Building for production

To bundle an application Dart uses so called "snapshots". It bundles all files in your project into an executable binary file. The Dart SDK can then execute the file.

Creating a snapshot:

$ dart --snapshot=<your-snapshot-name> <your-main-file>

Then run the created snapshot:

$ dart <your-snapshot-name>

4. Programming in Dart


There are three types of comments:

// Single line comment

Multi line

/// Documentation comments
/// These allow to reference types or variables via [square-brackets]
When you use documentation comments (///) generating Dartdoc can easily be done by using the dartdoc tool. This is distributed with the SDK.

4.2. Variables

Dart has type inference. This means there is a var keyword that tells the compiler, that it should determine the variable type from the return value. But variables can also be manually typed though it is favorable to use var in many cases.

var someVariable = "";
int someOtherVariable = 1;

4.2.1. static, final, const

static variables are available on the class itself instead of the instance.

final variables can not be reassigned. They must be initialized.

const means that the value of a variable can not be changed. This applies to List`s, classes and other types. Essentially the value of a `const variable is determined at compile time and can not be changed whatsoever at run time.

4.3. Classes

Since Dart is an object oriented language it has classes.

class ClassName {


4.3.1. Constructors

Constructors are called upon instantiation of the class.

Populating fields in the constructor is very simple:

class ClassName {

    String someVar;

    ClassName(this.someVar); (1)
1 This automatically requires that one parameter of type String is passed to the constructor.

Optional parameter in constructors are also possible:

class ClassName {

    String someVar;

    ClassName({this.someVar}); (1)
1 Note the curly braces. This marks the parameter someVar as optional and the programmer can decide whether to populate the field or not.

4.3.2. Instantiation of a Class

A class can be instantiated using the new keyword:

new ClassName("Some Parameter");

Optional parameters need to be named:

new ClassName(someVar: "Some Value");
Since Dart 2 the new keyword can be omitted.

If the members of a class are final and are initialized in the constructor it can also be made const. This allows the class to be constructed at compile time.

class ClassName {

    final String someVar;

    const ClassName({this.someVar});

4.3.3. Inheritance

Dart classes have mixin-based inheritance. Every object is an instance of a class, and all classes inherit from Object.

5. Exercise - Creating a first Dart project

In this exercise you will create a small Dart project, that performs a REST call and prints the output to the console. The call will extract the open change requests for the eclipse.platform.ui Gerrit repository.

Create the following gerrit.dart file:

import 'dart:io';
import 'dart:convert';

void main() {
    .getUrl(Uri.parse('https://git.eclipse.org/r/changes/?q=status:open+project:platform/eclipse.platform.ui')) // produces a request object
    .then((request) => request.close()) // sends the request
    .then((response) => response.transform(Utf8Decoder()).listen(print)); // transforms and prints the response


Execute the file using $ dart gerrit.dart.

6. Exercise: Use libraries in Dart

This exercise describes how to use the pubspec.yaml file to configure a Dart project.

Create the following pubspec.yaml file.

name: example
author: Dart Team <misc@dartlang.org>

  sdk: '>=2.2.0 <3.0.0'

  json_annotation: ^2.4.0

  build_runner: ^1.0.0
  json_serializable: ^3.0.0

  # Used by tests. Not required to use `json_serializable`.
  build_verify: ^1.0.0
  path: ^1.5.1
  test: ^1.6.0

Run $ pub get in the directory of your project to update the dependencies for your Dart library.

You should now be able to use the libraries.

import 'dart:io';
import 'dart:convert';

class Gerrit {
    final String subject;
    final String created;

    Gerrit(this.subject, this.created);
    Gerrit.fromJson(Map<String, dynamic> json)
      : subject = json['name'],
        created = json['email'];

void main() {
    .getUrl(Uri.parse('https://git.eclipse.org/r/changes/?q=status:open+project:platform/eclipse.platform.ui')) // produces a request object
    .then((request) => request.close()) // sends the request
    .then((response) => response.transform(Utf8Decoder()).listen(print)); // transforms and prints the response

7. Dart resources

8. vogella training and consulting support

Copyright © 2012-2019 vogella GmbH. Free use of the software examples is granted under the terms of the Eclipse Public License 2.0. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.