3 minute read

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


void main() {
  print('Hello, World!');
}

void is a special type that indicates a value that’s never used.

Functions like main() that don’t explicitly return a value have the void return type.

Variables


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'
};

Variables store references. The variable called name contains a reference to a String object with a value of “Voyager I”.

The type of the name variable is inferred to be String, but you can change that type by specifying it.

If an object isn’t restricted to a single type, specify the Object or dynamic type

Uninitialized variables have an initial value of null. Even variables with numeric types are initially null, because numbers, like everything else in Dart, are objects.

Final and const

If you never intend to change a variable, use final or const, either instead of var or in addition to a type.

A final variable can be set only once; a const variable is a compile-time constant. Const variables are implicitly final. A final top-level or class variable is initialized the first time it’s used.

Built-in types

The Dart language has special support for the following types:

  • numbers
  • strings
  • booleans
  • lists (also known as arrays)
  • sets
  • maps
  • runes (for expressing Unicode characters in a string)
  • symbols

You can initialize an object of any of these special types using a literal. For example, ‘this is a string’ is a string literal, and true is a boolean literal.

Because every variable in Dart refers to an object—an instance of a class—you can usually use constructors to initialize variables. Some of the built-in types have their own constructors.

For example, you can use the Map() constructor to create a map.

Lists

Perhaps the most common collection in nearly every programming language is the array, or ordered group of objects. In Dart, arrays are List objects, so most people just call them lists.

Lists use zero-based indexing, where 0 is the index of the first value and list.length - 1 is the index of the last value. You can get a list’s length and refer to list values just as you would in JavaScript

var list = [1, 2, 3];

Sets

A set in Dart is an unordered collection of unique items. Dart support for sets is provided by set literals and the Set type.

var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};

Maps

In general, a map is an object that associates keys and values. Both keys and values can be any type of object. Each key occurs only once, but you can use the same value multiple times. Dart support for maps is provided by map literals and the Map type.


var gifts = {
  // Key:    Value
  'first': 'partridge',
  'second': 'turtledoves',
  'fifth': 'golden rings'
};

Functions

Dart is a true object-oriented language, so even functions are objects and have a type, Function. This means that functions can be assigned to variables or passed as arguments to other functions. You can also call an instance of a Dart class as if it were a function. For details, see Callable classes.


bool isNoble(int atomicNumber) {
  return _nobleGases[atomicNumber] != null;
}

Control flow statements

Dart supports the usual control flow statement.



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;
}

Comments

Dart comments usually start with //.


// 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. */

Imports

To access APIs defined in other libraries, use import.


// 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';

Exceptions

To raise an exception, use throw:


if (astronauts == 0) {
  throw StateError('No astronauts.');
}

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


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();
}

Tags:

Categories:

Updated: