Home Tutorials Training Consulting Products Books Company Donate Contact us









vogella commercial offerings

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 has a wide variety of appliances: Mobile app development (using Flutter), web development (using Dart2JS) or server side (using various libraries and frameworks).

The object oriented language is very approachable for developers from various different languages as it’s syntax is close to Java or C++.

Dart is also the programming language of choice for the Flutter SDK an SDK to from Google to develop crossplatform mobile applications.

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.

2.1. Other 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.

2.2. Linux

On Linux this is typically 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.

2.3. MacOS

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

3. Development

To develop and run a Dart application you typically only need the SDK on your Path and a text editor. Such tools are Visual Studio Code or Atom.

3.1. Running a Dart application

Running a Dart program simply needs a file with a main() method. This file can then be executed using the Dart compiler via $ 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 your 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

4.1. Comments

There are three types of comments:

// Single line comment

/*
Multi line
comment
*/

/// 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. Dart resources

Not yet listed == 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.