Dart Language

Dart is a client-optimized language for fast apps on any platform.

DartPad is an open source tool that lets you play with the Dart language in any modern browser.

History

Version Date
1.0 2013-11
2.0 2018-08

Documentation

Features

  • Type inference
  • Null safety
  • Type safety
  • async and await
  • isolate-based concurrency
  • single inheritance
  • no interface, all classes implicitly define an interface
  • new keyword is optional
  • null coalescing operator ??
  • Cascade notation .. ?.., Assignment operators ??=
  • String interpolation
  • spread operator
  • collection if

  • Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects.
  • Although Dart is strongly typed, type annotations are optional because Dart can infer types.
  • If you enable null safety, variables can’t contain null unless you say they can.You can make a variable nullable by putting a question mark (?) at the end of its type.
  • If you know that an expression never evaluates to null but Dart disagrees, you can add ! to assert that it isn’t null.
  • Dart supports generic types, like List<int> or List<Object>.
  • You can also create functions within functions (nested or local functions).
  • doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore (_), it’s private to its library.
  • Dart has both expressions (which have runtime values) and statements (which don’t).
  • Dart tools can report two kinds of problems: warnings and errors.

The libraries

  • Built-in types, collections, and other core functionality for every Dart program (dart:core)
  • Richer collection types such as queues, linked lists, hashmaps, and binary trees (dart:collection)
  • Encoders and decoders for converting between different data representations, including JSON and UTF-8 (dart:convert)
  • Mathematical constants and functions, and random number generation (dart:math)
  • File, socket, HTTP, and other I/O support for non-web applications (dart:io)
  • Support for asynchronous programming, with classes such as Future and Stream (dart:async)
  • Lists that efficiently handle fixed-sized data (for example, unsigned 8-byte integers) and SIMD numeric types (dart:typed_data)
  • Foreign function interfaces for interoperability with other code that presents a C-style interface (dart:ffi)
  • Concurrent programming using isolates — independent workers that are similar to threads but don’t share memory, communicating only through messages (dart:isolate)
  • HTML elements and other resources for web-based applications that need to interact with the browser and the Document Object Model (DOM) (dart:html)

Syntax

Built-in types

Variables

1
2
3
var name = 'Bob';
Object name = 'Bob';
String name = 'Bob';
  • Variables store references.
  • the style guide recommendation of using var, rather than type annotations, for local variables.

Define a function

1
2
3
4
5
6
7
8
void printElement(int element) {
print(element);
}

var list = [1, 2, 3];

// Pass printElement as a parameter.
list.forEach(printElement);
  • Named parameters are optional unless they’re specifically marked as required.
  • Wrapping a set of function parameters in [] marks them as optional positional parameters.
  • Your function can use = to define default values for both named and positional parameters. The default values must be compile-time constants.
  • Every app must have a top-level main() function, which serves as the entrypoint to the app.
  • All functions return a value. If no return value is specified, the statement return null.
  • You can pass a function as a parameter to another function.

Anonymous functions

1
2
3
4
5
6
const list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
print('${list.indexOf(item)}: $item');
});

list.forEach((item) => print('${list.indexOf(item)}: $item'));
  • You can also create a nameless function called an anonymous function, or sometimes a lambda or closure.

Control flow statements

If the object that you are iterating over is an Iterable (such as List or Set) and if you don’t need to know the current iteration counter, you can use the for-in form of iteration:

1
2
3
for (var candidate in candidates) {
candidate.interview();
}

Exceptions

In contrast to Java, all of Dart’s exceptions are unchecked exceptions. Methods don’t declare which exceptions they might throw, and you aren’t required to catch any exceptions.

1
2
3
throw FormatException('Expected at least 1 section');

throw 'Out of llamas!';

Classes

Command-line and server apps

Write command-line apps

Create a small app

Use the dart create command and the console-full template to create a command-line app:

1
dart create -t console-full cli

Run the app

1
2
cd cli
dart run

Compile for production

Use the dart compile tool to AOT compile the program to machine code:

1
dart compile exe bin/cli.dart

Functions

The => expr syntax is a shorthand for { return expr; }. The => notation is sometimes referred to as arrow syntax.

OOP

Factory constructors

Asynchronous programming: futures, async, await

  • To define an async function, add async before the function body.
  • The await keyword works only in async functions.

Future class

1
2
3
Future<int> future = getFuture();
future.then((value) => handleValue(value))
.catchError((error) => handleError(error));

FutureBuilder class