Introduction
A .dart file holds source code in Dart, the
language Google announced in 2011. It compiles to native machine code and to JavaScript
and WebAssembly, and is best known as the language of the
Flutter interface toolkit.
A project is described by a pubspec.yaml file, which names its packages
from the pub.dev repository. Application code usually sits under lib/.
This reference is partial. It covers Dart language version 3.13 (SDK 3.13.7), and so far has one page, File structure. What it leaves out is listed under Not described.
Structure
A Dart file is UTF-8 text. Each file is normally a library of its own, and a file that
starts with part of belongs to another. import lines come
first, with URIs such as dart:io for the core libraries and
package:path/path.dart for packages. After them come classes, functions and
variables, all allowed at the top level. A program starts at main().
Names beginning with an underscore are private to their library. Statements end with
semicolons. Comments are //, /* */ and /// for
documentation. A type can hold null only if it is marked with
?, as in String?.
import 'dart:math';
class Point {
final double x, y;
const Point(this.x, this.y);
double get distance => sqrt(x * x + y * y);
}
void main() {
final p = Point(3, 4);
print('Distance: ${p.distance}');
}
The words on and of in the viewer
on and of are valid names in Dart, so the viewer colours them as
keywords only where the language gives them a meaning. on is a keyword in a
catch clause (} on FormatException catch (e) {, or on StateError {
at the start of a line) and in an extension or mixin declaration
(extension E on String, mixin M on Base). of is a
keyword only in part of. A variable called on or of,
List.of(x) and button.on('click') show no keyword.
Not described
Known, not yet covered
- Reference: the reference has one page, File structure. The language itself has no pages yet: lexical structure, types, variables, expressions, collections, records, patterns, statements and control flow, error handling, functions, classes, constructors, class modifiers, mixins, enums, extensions, generics, metadata, libraries and packages, concurrency, the standard library and the version history.
- Reference: only Dart 3.13 is covered. There is no history of earlier language versions (Dart 1, sound null safety in 2.12, records and patterns in 3.0), and
pubspec.yaml, thedartandfluttercommands and Flutter’s widget code are named but not described. - Viewer: the words
get,set,show,hide,in,whenandlateare coloured as keywords wherever they appear, so a variable with one of those names is coloured as a keyword. Only the core library types are coloured as types (int,String,List,Futureand a few others); a class you declare, and a generic parameter such asT, are plain. - Viewer: the code inside
${ }of an interpolated string is part of the string’s colour and is not scanned as Dart. - Viewer: the file is not parsed or analysed: there is no outline, no list of problems, no null-safety, type or import checking, and
importandpartURIs are not followed. Only the first 1 MiB of a file is read and coloured.
Links
Software
- Dart SDK – the official compiler, runtime and
dartcommand (Windows, macOS, Linux) - Flutter – interface toolkit that includes the Dart SDK (Windows, macOS, Linux)
- Android Studio – development environment with Dart and Flutter plugins (Windows, macOS, Linux)
- Visual Studio Code – free code editor with a Dart extension (Windows, macOS, Linux)
Developers
- analyzer – the package that parses and analyses Dart source (Dart)
- dart_style – the package behind the
dart formatcommand (Dart)
Reference
- Dart language documentation – the official guide to the language
- Dart language specification – the formal specification and its drafts
- ECMA-408 – the Ecma standard for Dart 1.x