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/.
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}');
}
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