Click here to load reader
Sep 21, 2018
Web Programming with Dart
Moises Belchin
Patricia Juberias
Web Programming with Dart
Copyright 2015 by Moises Belchin and Patricia Juberias
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publishers location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4842-0557-0
ISBN-13 (electronic): 978-1-4842-0556-3
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director: Welmoed SpahrLead Editor: Ben Renow-ClarkeTechnical Reviewer: Matthew ButlerEditorial Board: Steve Anglin, Mark Beckner, Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan Gennick,
Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Matt Wade, Steve Weiss
Coordinating Editor: Melissa MaldonadoCopy Editor: Lori Cavanaugh and April RondeauCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk SaleseBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your books source code, go to www.apress.com/source-code/.
http://[email protected]://[email protected]/bulk-saleswww.apress.comwww.apress.com/source-code/
iii
Contents at a Glance
About the Authors xv
About the Technical Reviewer xvii
Acknowledgments xix
Introduction xxi
Chapter 1: Darts Flightpath So Far 1
Part I: Getting Started 13
Chapter 2: Setting up Dart Correctly 15
Part II: Dart Tools 31
Chapter 3: Making the Most of the Dart Editor 33
Chapter 4: Using Pub and dart2js to Compile Applications to JavaScript 75
Chapter 5: Application Launches 87
Chapter 6: Using the Dart Pub Package Manager 99
Chapter 7: Optimizing for Dart VM and Dartium 115
Chapter 8: Cleaning up with Dart Formatter 121
Chapter 9: Generating Dart Code Documentation 125
Chapter 10: Monitoring Your Applications with Observatory 135
Part III: The Dart Language: Basics 147
Chapter 11: The Dart Comment System 149
Chapter 12: Understanding Operators and Expressions 159
Chapter 13: Mastering Darts Variables and Data Types 171
Contents at a GlanCe
iv
Chapter 14: Flow Control Statements 205
Chapter 15: Working with Functions 211
Chapter 16: A Syntax Summary and Cheat Sheet 219
Part IV: The Dart Language: Advanced 223
Chapter 17: Processing Exceptions and Error Control 225
Chapter 18: Understanding Dart Classes 229
Chapter 19: Implicit Interfaces and Abstract Classes 243
Chapter 20: Implementing Generics and Typedefs 251
Chapter 21: Using Darts Libraries 257
Chapter 22: Leveraging Isolates for Concurrency and Multi-Processing 273
Chapter 23: Asynchronous Programming with Dart 279
Chapter 24: An Advanced Syntax Summary 299
Part V: Dart and Other Web Technologies 305
Chapter 25: Integrating Dart and HTML5 307
Chapter 26: Integrating Dart and CSS 321
Part VI: Dart and the Web Server 339
Chapter 27: Combining Web Services with Dart 341
Chapter 28: Dart on the Server Side 367
Part VII: Darts Future 387
Chapter 29: Implementing Design Patterns and Creating Web Components 389
Chapter 30: Developing a Dart App 407
Index 437
v
Contents
About the Authors xv
About the Technical Reviewer xvii
Acknowledgments xix
Introduction xxi
Chapter 1: Darts Flightpath So Far 1
What is Dart? 1
Advantages to Using Dart 3
Darts Origins 3
Converting From Other Programming Languages 5
Dart Names are Simpler and More Structured 5
Searching for DOM Elements is Easier 5
Dart Has Real Data Collections 6
Constructors to Create New DOM Elements 6
Dart Improves and Unifies the Events System 7
Say Goodbye to Vendor Prefixes 7
Future-Based APIs 7
Libraries 8
Cross-Browser Consistency 8
Cascaded DOM Construction 9
Why Dart is Perfect for Web Development 9
Summary 11
Contents
vi
Part I: Getting Started 13
Chapter 2: Setting up Dart Correctly 15
Downloading and Installing the Software 15
Setting Up a Previous Version of Dart Editor 17
Dart Editor and the Software Development Environment 20
Opening and Running an Example 21
Creating Your First Application 24
Creating a Web Application 24
Creating Command-Line Applications 26
Summary 29
Part II: Dart Tools 31
Chapter 3: Making the Most of the Dart Editor 33
The Dart Editor in Detail 33
Opening Some Examples36
Creating Applications 38
Dart Editor Options and Main Views 40
Preferences and Views 46
Running Applications 57
How Does this Sample Application Run? 59
Advanced Examples 60
Debugging Applications 64
Inspecting the SDK 70
Summary 73
Chapter 4: Using Pub and dart2js to Compile Applications to JavaScript 75
Reviewing the Compliation Process 75
Pub build 77
dart2js 80
Pub Serve 84
Summary 86
Contents
vii
Chapter 5: Application Launches 87
Manage Launches 87
Dart Command-Line Launch89
Dartium Launch 91
Chrome App Launch 92
Browser Launch 94
Android Mobile Launch 95
Summary 98
Chapter 6: Using the Dart Pub Package Manager 99
What is Pub? 99
Digging Deeper 99
Working With Commands and Packages 102
Additional Pub Commands 107
Summary 113
Chapter 7: Optimizing for Dart VM and Dartium 115
Dart Virtual Machine 115
Dartium117
Checked Mode 117
Summary 119
Chapter 8: Cleaning up with Dart Formatter 121
Dart Formatter 121
Summary 124
Chapter 9: Generating Dart Code Documentation 125
docgen125
DocGen Options 127
Summary 134
Contents
viii
Chapter 10: Monitoring Your Applications with Observatory 135
Observatory 135
Observatory and Command-Line Applications 136
Observatory and Web Applications 139
Summary 145
Part III: The Dart Language: Basics 147
Chapter 11: The Dart Comment System 149
Comments Overview 149
Inline Comments 149
Block Comments 150
Documentation Comments 150
Markdown 153
Links 154
Block Styles 155
Inline Styles 156
Summary 157
Chapter 12: Understanding Operators and Expressions 159
An Introduction to Dart Operators 159
Operator Types 160
Arithmetic 161
Equality and Relational 162
Type Test 162
Assignment 163
Conditional 164
Bit 165
Others 165
An Introduction to Expressions 166
Conditional 166
Logical 166
Bit 166