Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

1. What is Dart? > Show Me the Code

Show Me the Code

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"

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial