Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Enough talk, let’s see some code. Whether you know JavaScript or Java, Dart code should look familiar. Here’s an example of a simple web page in Dart:
hi.dart:
#import('dart:html');
main() {
document.query('#status').text = 'Hi, Dart';
}
hi.html:
... <h2 id="status"></h2> <script type="application/dart" src="hi.dart"></script> <!-- If the browser doesn't have an embedded Dart VM, you can compile Dart code to JavaScript. --> <script type="text/javascript" src="hi.dart.js"></script> ...
Now let’s look at some Dart code that uses functions:
send(msg, to, from, [rate='First Class']) {
return '${from} said ${msg} to ${to} via ${rate}';
}
main() => print(send('hello', 'Seth', 'Bob'));
> "Bob said hello to Seth via First Class"