Learning Dart Learn how to program applications with Dart 1.0, a language specifically designed to produce better-structured, high-performance applications Ivo Balbaert Dzenan Ridjanovic BIRMINGHAM - MUMBAI Learning Dart Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: December 2013 Production Reference: 1191213 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84969-742-2 www.packtpub.com Cover Image by Javier Barría C ([email protected]) Credits Authors Copy Editors Ivo Balbaert Roshni Banerjee Dzenan Ridjanovic Brandt D'Mello Tanvi Gaitonde Reviewers Shambhavi Pai Sergey Akopkokhyants Lavina Pereira Tom Alexander Adithi Shetty Christophe Herreman Laxmi Subramanian Yehor Lvivski Christopher McGuire Project Coordinators Apeksha Chitnis Acquisition Editors Wendell Palmer Saleem Ahmed Rebecca Youe Proofreader Clyde Jenkins Lead Technical Editor Neeshma Ramakrishnan Indexer Monica Ajmera Mehta Technical Editors Venu Manthena Graphics Amit Singh Ronak Dhruv Hardik B. Soni Disha Haria Gaurav Thingalaya Production Coordinator Pooja Chiplunkar Cover Work Pooja Chiplunkar About the Authors Ivo Balbaert is currently a lecturer for (Web) Programming and Databases at CVO Antwerpen (www.cvoantwerpen.be), a community college in Belgium. He received a Ph.D. in Applied Physics from University of Antwerp in 1986. He worked for 20 years in the software industry as a developer and consultant in several companies, and for 10 years as a project manager at the University Hospital of Antwerp. From 2000 onward, he switched to partly teaching and partly developing software (KHM Mechelen, CVO Antwerp). He also wrote an introductory book in Dutch about developing in Ruby and Rails: Programmeren met Ruby en Rails, Van Duuren Media, ISBN: 978-90-5940-365-9, 2009, 420 p. In 2012, he authored a book on the Go programming language: The Way To Go, IUniverse, ISBN: 978-1-4697-6917-2, 600 p. Dzenan Ridjanovic is a university professor who is planning his early retirement to focus on the development of web applications with Dart, HTML5, web components, and NoSQL databases. For more than 10 years, he was a Director of Research and Development in the Silverrun team (http://www. silverrun.com/), which created several commercial tools for analysis, design, and development of data-driven applications. He was a principal developer of Modelibra (http://www.modelibra.org/) tools and frameworks for model-driven development in Java. Recently, he has been developing the Dartling framework for design and code generation of Dart models. His projects are at GitHub (https:// github.com/dzenanr), where he is considered a Dart expert (http://osrc.dfm. io/dzenanr). He writes about his projects at On Dart blog (http://dzenanr. github.io/). His courses are available at On Dart Education (http://ondart. me/). He markets his Dart efforts at On Dart G+ Page (https://plus.google. com/+OndartMe). Dzenan Ridjanovic wrote a book in 2009, under the Creative Commons License, entitled Spiral Development of Dynamic Web Applications: Using Modelibra and Wicket (http://www.modelibra.org/). I want to thank my spouse Amra for her constant care and support, the Dart team for creating a missing web language, and Ivo Balbaert for his productive writing. About the Reviewers Sergey Akopkokhyants is a Java certified technical architect with more than 19 years of experience in designing and developing client and server-side applications. For the last five years, Sergey has been responsible for customizing and producing web-oriented applications for wholesale business management solutions projects for several worldwide mobile communications companies. Sergey is passionate about web designing and development, and believes that an investment in bleeding edge technologies will always give a return to both the individual and the organization. He is also the author and owner of several open source projects on GitHub, including the Dart Web Toolkit (DWT). Sergey likes sharing knowledge and experience with others, and helping them to increase their skills and productivity. Tom Alexander graduated with a Computer Science degree from Rensselaer Polytechnic Institute. He currently works for TripAdvisor as a software engineer, where he works on the mobile version of the website. Christophe Herreman is a versatile and passionate software architect with more than 10 years of professional experience. He is also a Certified Scrum Master and has general knowledge of many tools and platforms. For the last few years, Christophe has mainly worked on web-based applications in a variety of domains, such as healthcare, education, traffic control, and electronics. His main tasks have been: architecture and development of the client software using Flex, ActionScript and Adobe AIR, HTML, JavaScript, and client-server integrations with Java, .NET, and PHP; automating the build process with tools such as Maven, Ant, Batch, Jenkins/ Hudson; setting up unit, integration and functional tests; reviewing, auditing, and improving existing codebases; coaching and mentoring teams. Christophe is also a regular speaker at conferences and user groups, as well as an active open source contributor. He's the founder of the Spring ActionScript framework and the AS3Commons project and a committer on the Apache Flex project. In the past, he has lectured on software and web development at the University College of West- Flanders, Belgium. Together with his partner, he runs the Belgium-based software consultancy firm Stack & Heap (www.stackandheap.com). Yehor Lvivski thinks that it's never too late to learn and always tries to know the current trends of the web and predict the future ones. He has always wanted to be a designer, but later found an area where he can combine both his design and engineering skills. For the last six years, Yehor was involved in game development, added a nice interactive UI for a search engine, created his own CSS animations library, and several open source projects. He really likes to create a great visual experience. Yehor now works for SpatialKey, and is involved in changing the future of data analysis. Not only does he like to create things, but he's also an active speaker at different local and global conferences. He believes that knowledge sharing is the key point to evolution. Christopher McGuire is an application developer currently working in Glasgow, Scotland. He graduated from the University of Strathclyde in 2011 with a B.Sc (Hons) in Computer Science and currently works for an investment bank. Chris has experience in developing Enterprise Standard Server applications, primarily using Java and other object oriented-languages, while also developing web/mobile front-end applications using native Android/iOS and HTML5. From this, he has multiple applications published on the Google Play market. In addition to this, Chris has developed multiple web applications and has a strong interest in new and emerging technologies, which have led him to become part of the Dart community and develop several more applications using this language. www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Dart – A Modern Web Programming Language 9 What is Dart? 9 Dart is a perfect fit for HTML5 10 A very short history of web programming 11 Dart empowers the web client 11 JavaScript is not the way to go for complex apps 12 Google, GWT, and Dart 13 Advantages of Dart 13 Getting started with Dart 14 Installing the Dart Editor 15 Your first Dart program 16 Getting a view on the Dart tool chain 18 The Dart execution model 18 A bird's eye view on Dart 20 Example 1 – raising rabbits 20 Extracting a function 22 A web version 24 Example 2 – banking 27 Making a todo list with Dart 28 Summary 29