Dart in Action
Chris Buckett
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editor: Susanna KlineTechnical proofreader: John EvansCopyeditor: Tiffany TaylorProofreader: Toma MulliganTypesetter: Gordan SalinovicCover designer: Marija Tudor |
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 17 16 15 14 13 12
Brief Table of Contents
Table of Contents
Foreword
When I heard that we were starting work on Dart, a structured and scalable language with a fast virtual machine, a powerful editor, and a compiler to JavaScript, at first I didnt believe it. Could this be the project to make web programming easier for developers like me? I hopefully wondered. Coming from a structured language background, and used to powerful developer tools, Id been waiting for a more productive way to build larger modern web apps. The Dart project sounded like just what I was looking for. I grew up on object-oriented languages like C++, Java, and Ruby as I first built interactive websites and then later rich client-side web apps. I learned to be productive with classes, objects, and modular code. I appreciated IDEs for their analysis, refactoring, and navigation capabilities because they helped me write more complex, larger applications. Life was great. Looking for a new opportunity, I was lucky enough to get a job working with the Chrome team. For the first time, I learned how to exploit the modern browser, and I dove into the many HTML5 features. The modern web evolves very quickly and reaches so many people that its an exciting place to be. Life was even better.
Although I loved the iterative and fast-paced nature of web development, I was missing my structured languages and helpful tools. I wanted a way to build for modern browsers with IDEs that could perform code completion, languages that had real classes, and more.
So when I heard about Dart, I jumped at the opportunity to help out. Build for the most exciting platform with a development experience that Im familiar and productive with? You bet!
I wasnt the only developer who immediately joined the fun. The author of this book, Chris Buckett, is one of our earliest adopters of Dart. He started the Dartwatch blog on the day that Google announced Dart, and its still going strong. Chris has been with the project since the beginning, so its only natural that he is one of the first to write a book to help other developers learn Dart.
Chris is some sort of super author, for he has been able to write this book as the project was going through numerous changes to its libraries and language. Hes done a great job covering the many different aspects and features of the Dart project. I especially enjoyed his numerous examples of not only the core language features, but also the more advanced HTML5 features. Chris embraces the single-page app and shows how to use Dart to build modern browser-based apps. Youll even learn how to program server-side Dart with this book!
After a year of hard work, tens of thousands of commits, thousands of bugs, and great community feedback, the dream of structured web programming is a reality. Although Dart isnt done yet, thanks to Chriss book, together we can have fun building great apps for the modern web. Enjoy!
S ETH L ADD
D EVELOPER A DVOCATE
G OOGLE
Preface
In October 2011, rumor became reality when Google released a new language aimed at developing complex, Google-scale web applications. An internal Google email titled Future of JavaScript had appeared on the web a month earlier, indicating that a language, possibly to be known as Dash, was undergoing development within Google, with the aim of being a better language for the web than JavaScript. Born out of frustration with the slow progress in evolving JavaScript, partly caused by the numerous interested parties and committees, this new language aimed to be everything JavaScript could be if it were invented now. Its key goal was to maintain the dynamic nature of JavaScript, but have a better performance profile and be amenable to tooling for large projects. It would also be able to cross-compile to JavaScript. This language was released as a technical preview to the wider world and given the name Dart.
I had just come out the back of a major GWT project at my employer, creating a bespoke document-referencing application designed for touch screens that would be deployed in non-computer-friendly environments. Google Web Toolkit (GWT) is a technology that Google created for cross-compiling Java to JavaScript. GWT lets developers benefit from the structure, type-safety, and tooling provided by Java, while still being able to target browsers natively without requiring plug-ins such as Flash or Silverlight. Having spent the last two years writing GWT code and coordinating developers across three countries, I knew the value of being able to use tooling to validate code at integration pointssomething that was lacking when trying to achieve the same with JavaScript. The ability to reuse code on both the client and the server also appealed to meI had seen the benefit.
Keen to know more about this new Dart language, I read all the documentation that was available. At the time, this consisted of the source code, some sample projects, and the language specification. It seemed that if I were to make the effort of getting the knowledge into my head, it would be worth sharing with the wider community through blog posts. I started the Dartwatch blog and shared a series of simple descriptions of how to achieve common tasks in Dart, such as how to organize a project, how to create classes, and how to interact with the browser. One thing led to another, and I was approached by Manning about the possibility of writing a book on Dart. Just over a year later, the result is in print.
Over the last year, Dart has had time to mature, and its developers have been listening and responding to feedback. Darts Milestone 1 release is imminent, and there have been many changes to the original language specification as a result of real-world use by the languages early adopters. A community of these early adopters has also been creating tools and libraries such as database drivers, 2D and 3D graphics libraries, and MVC frameworks, many of which can be found on GitHub or on the Dartwatch website.