Dart brings structure to web app engineering with a new language, libraries, and tools. Read about our new M2 release!
Familiar
Dart is a class-based, object-oriented language with lexical scoping, closures, and optional static typing. Dart helps you build structured modern web apps and is easy to learn for a wide range of developers. Learn more
import 'dart:html';
main() {
var msg = query('#msg');
var btn = new ButtonElement();
btn.text = 'Click me!';
btn.on.click.add((e) => msg.text = 'Dart!');
document.body.nodes.add(btn);
}
import 'dart:html';
main() {
var msg = query('#msg');
var btn = new ButtonElement();
btn.text = 'Click me!';
btn.on.click.add((e) {
msg.text = 'Dart!';
});
document.body.nodes.add(btn);
}
Productive
Dart Editor and the SDK provide an integrated development and debugging experience. The editor supports refactoring, breakpoints, code completion, code navigation, and more. The SDK contains the stand-alone virtual machine, a package manager, and Chromium with an embedded Dart VM. Learn more
Ubiquitous
Dart can be compiled to JavaScript, so you can use it for web apps in all modern desktop and mobile browsers. Our JavaScript compiler generates minimal code thanks to tree-shaking. Dart apps can also run on the server, in a stand-alone Dart VM.
Learn
Overview
A structured language that's flexible and familiar
Dart addresses issues with traditional web development languages while remaining easy to learn. Thanks to optional static types, Dart scales from simple scripts to large apps. Learn more with the language tour, or read the language spec.
import 'dart:math';
class Point {
final num x, y;
Point(this.x, this.y);
Point.zero() : x = 0, y = 0; // Named constructor
// with an initializer list.
num distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
Classes
Dart supports classes as a fundamental structural building block for libraries and apps. Classes define the structure of an object, and you can extend them to create more specialized definitions. New features such as implicit interfaces and named constructors make it easier to say more while typing less. Learn more about classes in Dart.
makeAdder(num x) {
return (num y) => x + y;
}
main() {
var add2 = makeAdder(2);
assert(add2(3) == 5);
}
Lexical closures
Dart has lexical scoping and a convenient closure
mechanism. The makeAdder
function returns a
new function that closes around x
.
The =>
syntax is syntactic
sugar for a one-line function with a return value.
// In widget.dart
library widget;
class Widget {
// ...
}
// In toolkit.dart
library toolkit;
import 'widget.dart';
class Window extends Widget {
// ...
}
Libraries
Use libraries to create reusable modules of functionality. Libraries can import other libraries, making it easy to publish and share code.
import 'dart:math';
class Point {
final num x, y;
Point(this.x, this.y);
// Use static types for
// the "surface area" of your code.
num distanceTo(Point other) {
// var declares an
// untyped dynamic variable
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
Optional static types
Think of Dart's static types as annotations and documentation that help clearly indicate your intent, without getting in your way. Use types when you want more help from your tools, or if you want to make it easier to understand how to use your code. Omit the type annotations if you're just scripting around or if you're trying to express something that is challenging to express with a single type annotation. Type annotations don't affect the runtime semantics. Learn more about optional types in Dart.
flipFlags({bool on, bool up, bool hidden=false}) {
// ...
}
flipFlags(on: true, up: false, hidden: true);
// Named params are also optional params
flipFlags(on: true); // up == null, hidden == false
Named parameters
The meaning of true
or false
is hard to decipher without context.
Using named parameters, you can write code that is easier
to understand.
As a bonus, named parameters are also optional parameters.
import 'dart:isolate';
echo() {
port.receive((msg, SendPort replyTo) {
replyTo.send("Echo: $msg");
});
}
main() {
SendPort echoService = spawnFunction(echo);
echoService.call("Hello!").then((answer) => print(answer));
}
Isolates
Create isolates for concurrent programming and to run third-party code more securely. Communicate between isolates by passing messages that are copied before received to ensure no state is ever shared. Dart uses isolates instead of shared-state threads for safer parallel programming.
The echo()
function is run in an isolate,
which is created with spawnFunction
.
A message is sent by call
and an answer
is printed.
The Dart language contains many more useful and productive features. Check out a sample:
- better asynchronous programming with Futures
- more explicit collections with generics
- better for loops
- error handling with exceptions
Learn more about the Dart language by taking a Language Tour, or reading the Dart Language Spec.
Productive and integrated tools for faster development
Gone are the days of building web apps with plain text editors. Dart Editor, its static analysis engine, and direct integration with Chromium+DartVM helps you develop, debug, and maintain your apps.
Dart also ships a stand-alone Dart SDK that contains the dart2js compiler, the Dart VM for running command-line apps, and the pub package manager.
-
Code completion
Explore the methods and fields available to the object you're working on.
-
Refactoring
Change your code structure without changing the behavior.
-
Outline view
List the classes, methods, and functions in a simple tree display.
-
Debugger
Set breakpoints, inspect variables, and step over, into, and out of code.
-
Static analysis
See warnings when inconsistencies and potential problems are detected.
-
Find callers
Quickly find all callers for a method, and easily jump to those locations.