Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
406 lines (305 sloc) 11.2 KB
title description
Sample Code
Examples of idiomatic Dart with links to larger examples.

This collection is not exhaustive—it's just a brief introduction to the language for people who like to learn by example. You may also want to check out the language and library tours.

Language tour

A comprehensive tour, with examples, of the Dart language. Most of the read more links in this page point to the language tour.

Library tour

An example-based introduction to the Dart core libraries. See how to use the built-in types, collections, dates and times, streams, and more.

Hello World

Every app has a main() function. To display text on the console, you can use the top-level print() function:

{% prettify dart %} void main() { print('Hello, World!'); } {% endprettify %}


Even in type-safe Dart code, most variables don't need explicit types, thanks to type inference:

{% prettify dart %} var name = 'Voyager I'; var year = 1977; var antennaDiameter = 3.7; var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune']; var image = { 'tags': ['saturn'], 'url': '//path/to/saturn.jpg' }; {% endprettify %}

Read more about variables in Dart, including default values, the final and const keywords, and static types.

Control flow statements

Dart supports the usual control flow statements:

{% prettify dart %} if (year >= 2001) { print('21st century'); } else if (year >= 1901) { print('20th century'); }

for (var object in flybyObjects) { print(object); }

for (int month = 1; month <= 12; month++) { print(month); }

while (year < 2016) { year += 1; } {% endprettify %}

Read more about control flow statements in Dart, including break and continue, switch and case, and assert.


We recommend specifying the types of each function's arguments and return value:

{% prettify dart %} int fibonacci(int n) { if (n == 0 || n == 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); }

var result = fibonacci(20); {% endprettify %}

A shorthand => (arrow) syntax is handy for functions that contain a single statement. This syntax is especially useful when passing anonymous functions as arguments:

{% prettify dart %} flybyObjects.where((name) => name.contains('turn')).forEach(print); {% endprettify %}

Besides showing an anonymous function (the argument to where()), this code shows that you can use a function as an argument: the top-level print() function is an argument to forEach().

Read more about functions in Dart, including optional parameters, default parameter values, and lexical scope.


Dart comments usually start with //.

{% prettify dart %} // This is a normal, one-line comment.

/// This is a documentation comment, used to document libraries, /// classes, and their members. Tools like IDEs and dartdoc treat /// doc comments specially.

/* Comments like these are also supported. */ {% endprettify %}

Read more about comments in Dart, including how the documentation tooling works.


To access APIs defined in other libraries, use import.

{% prettify dart %} // Importing core libraries import 'dart:math';

// Importing libraries from external packages import 'package:test/test.dart';

// Importing files import 'path/to/my_other_file.dart'; {% endprettify %}

Read more about libraries and visibility in Dart, including library prefixes, show and hide, and lazy loading through the deferred keyword.


Here's an example of a class with three properties, two constructors, and a method. One of the properties can't be set directly, so it's defined using a getter method (instead of a variable).

{% comment %} The linter rule sort_constructors_first made us put the getter below the constructors: {% endcomment %}

{% prettify dart %} class Spacecraft { String name; DateTime launchDate;

// Constructor, with syntactic sugar for assignment to members. Spacecraft(, this.launchDate) { // Initialization code goes here. }

// Named constructor that forwards to the default one. Spacecraft.unlaunched(String name) : this(name, null);

int get launchYear => launchDate?.year; // read-only non-final property

// Method. void describe() { print('Spacecraft: $name'); if (launchDate != null) { int years = ~/ 365; print('Launched: $launchYear ($years years ago)'); } else { print('Unlaunched'); } } } {% endprettify %}

You might use the Spacecraft class like this:

{% prettify dart %} var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5)); voyager.describe();

var voyager3 = Spacecraft.unlaunched('Voyager III'); voyager3.describe(); {% endprettify %}

Read more about classes in Dart, including initializer lists, optional new and const, redirecting constructors, factory constructors, getters, setters, and much more.


Dart has single inheritance.

{% prettify dart %} class Orbiter extends Spacecraft { num altitude; Orbiter(String name, DateTime launchDate, this.altitude) : super(name, launchDate); } {% endprettify %}

Read more about extending classes, the optional @override annotation, and more.


Mixins are a way of reusing code in multiple class hierarchies. The following class can act as a mixin:

{% prettify dart %} class Piloted { int astronauts = 1; void describeCrew() { print('Number of astronauts: $astronauts'); } } {% endprettify %}

To add a mixin's capabilities to a class, just extend the class with the mixin.

{% prettify dart %} class PilotedCraft extends Spacecraft [!with!] Piloted { // ··· } {% endprettify %}

Orbiter now has the astronauts field as well as the describeCrew() method.

Read more about mixins.

Interfaces and abstract classes

Dart has no interface keyword. Instead, all classes implicitly define an interface. Therefore, you can implement any class.

{% prettify dart %} class MockSpaceship implements Spacecraft { // ··· } {% endprettify %}

Read more about implicit interfaces.

You can create an abstract class to be extended (or implemented) by a concrete class. Abstract classes can contain abstract methods (with empty bodies).

{% prettify dart %} [!abstract!] class Describable { void describe();

void describeWithEmphasis() { print('========='); describe(); print('========='); } } {% endprettify %}

Any class extending Describable has the describeWithEmphasis() method, which calls the extender's implementation of describe().

Read more about abstract classes and methods.


Avoid callback hell and make your code much more readable by using async and await.

{% prettify dart %} const oneSecond = Duration(seconds: 1); // ··· Future printWithDelay(String message) [!async!] { await Future.delayed(oneSecond); print(message); } {% endprettify %}

The method above is equivalent to:

{% prettify dart %} Future printWithDelay(String message) { return Future.delayed(oneSecond).then((_) { print(message); }); } {% endprettify %}

As the next example shows, async and await help make asynchronous code easy to read.

{% prettify dart %} Future createDescriptions(Iterable objects) async { for (var object in objects) { try { var file = File('$object.txt'); if (await file.exists()) { var modified = await file.lastModified(); print( 'File for $object already exists. It was modified on $modified.'); continue; } await file.create(); await file.writeAsString('Start describing $object in this file.'); } on IOException catch (e) { print('Cannot create description for $object: $e'); } } } {% endprettify %}

You can also use async*, which gives you a nice, readable way to build streams.

{% prettify dart %} Stream report(Spacecraft craft, Iterable objects) async* { for (var object in objects) { await Future.delayed(oneSecond); yield '${} flies by $object'; } } {% endprettify %}

Read more about asynchrony support, including async functions, Future, Stream, and the asynchronous loop (await for).


To raise an exception, use throw:

{% prettify dart %} if (astronauts == 0) { throw StateError('No astronauts.'); } {% endprettify %}

To catch an exception, use a try statement with on or catch (or both):

{% prettify dart %} try { for (var object in flybyObjects) { var description = await File('$object.txt').readAsString(); print(description); } } on IOException catch (e) { print('Could not describe object: $e'); } finally { flybyObjects.clear(); } {% endprettify %}

Note that the code above is asynchronous; try works for both synchronous code and code in an async function.

Read more about exceptions, including stack traces, rethrow, and the difference between Error and Exception.

Other topics

Many more code samples are in the Language Tour and the Library Tour. Also see the API reference, which often contains examples.