Dart aims to be unsurprising,
but it does add a few language features
to keep your code succinct yet understandable.
Read all about Dart syntax in the
Dart language tour.
import 'dart:math'; // Import a library.
// Functions and variables can be inside or outside of classes.
var shapes = new List(); // A global variable.
addShape(shape) =>
shapes.add(shape); // Function shorthand syntax.
// Every app has a main() function, where execution starts.
main()
{
// The cascade operator (..) saves you from repetitive typing.
addShape(new Ellipse(10, 20)..
rotation = 45*PI/180
..
color = 'rgb(0,129,198)'
..
outlineWidth = 0);
// You can easily insert expression values into strings.
print('Area of the first shape: ${shapes[0].area}
');
}
Declaring types is optional in Dart.
These types don't affect the runtime behavior,
but you can use them to clarify APIs
and get better help from tools—for example,
better code completion, easier navigation, and earlier error detection.
For details, see
Optional Types in Dart.
// Without types.
recalculate(origin, offset, estimate) {
// ...
}
// With types (and an optional parameter).
num
recalculate(Point
origin,
num
offset,
{bool
estimate:false}) {
// ...
}
Dart is object oriented, with full support for classes.
Each class has a single superclass,
but classes can implement any number of interfaces.
For details, see
Classes in the language tour.
import 'dart:math';
class
Ellipse extends Shape { // Declare a class.
num majorAxis = 0; // An instance variable (property).
num minorAxis = 0;
static const num C = PI/4; // A constant.
num get area => C*majorAxis*minorAxis; // A property implemented with a getter.
Ellipse(this.majorAxis
, this.minorAxis
); // Compact constructor syntax.
Ellipse.circle(num diameter) { // A named constructor.
minorAxis = majorAxis = diameter;
}
}
abstract
class Shape { // Declare a class that can't be instantiated.
num get area;
num rotation = 0;
num outlineWidth = 1;
String color = 'black';
}
Functions that do potentially expensive work return Futures.
To specify what happens when the function's work is done,
invoke then()
on the Future.
To handle errors, invoke catchError()
.
Learn more by reading
Using Future Based APIs.
import 'dart:io';
import 'dart:async';
void printDailyNewsDigest() {
File file = new File("dailyNewsDigest.txt");
file.readAsString()
// readAsString() returns a Future.
.then
((content) => print(content)) // Handle successful completion.
.catchError
((error) => // Handle failure.
print("Sorry, no news today. Here's why:\n$error"));
}
Here's an example of implementing a web app using the
polymer.dart
package.
<head>
<link rel="import" href="click_counter.html
">
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<click-counter></click-counter>
</body>
<polymer-element name="click-counter
">
<template>
<button on-click="increment">Click Me</button>
<p>You clicked the button {{count}} times.</p>
</template>
<script type="application/dart">
import 'package:polymer/polymer.dart';
import 'dart:html';
@CustomTag('click-counter')
class ClickCounterElement extends PolymerElement with ObservableMixin {
@observable int count = 0;
void increment(Event e, var detail, Node target) {
count += 1;
}
}
</script>
</polymer-element>