Dart in Action
Total Page:16
File Type:pdf, Size:1020Kb
Chris Buckett FOREWORD BY Seth Ladd MANNING www.allitebooks.com Dart in Action www.allitebooks.com www.allitebooks.com Dart in Action CHRIS BUCKETT MANNING Shelter Island www.allitebooks.com 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: [email protected] ©2013 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 Manning’s 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. Development editor: Susanna Kline Manning Publications Co. Technical proofreader: John Evans 20 Baldwin Road Copyeditor: Tiffany Taylor PO Box 261 Proofreader: Toma Mulligan Shelter Island, NY 11964 Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617290862 Printed in the United States of America 12345678910–MAL –171615141312 www.allitebooks.com brief contents PART 1INTRODUCING DART .........................................................1 1 ■ Hello Dart 3 2 ■ “Hello World” with Dart tools 24 3 ■ Building and testing your own Dart app 40 PART 2CORE DART.................................................................. 69 4 ■ Functional first-class functions and closures 71 5 ■ Understanding libraries and privacy 94 6 ■ Constructing classes and interfaces 119 7 ■ Extending classes and interfaces 138 8 ■ Collections of richer classes 158 9 ■ Asynchronous programming with callbacks and futures 183 PART 3CLIENT-SIDE DART APPS.................................................209 10 ■ Building a Dart web app 211 11 ■ Navigating offline data 237 12 ■ Communicating with other systems and languages 258 v vi BRIEF CONTENTS PART 4SERVER-SIDE DART ........................................................281 13 ■ Server interaction with files and HTTP 283 14 ■ Sending, syncing, and storing data 308 15 ■ Concurrency with isolates 331 contents foreword xv preface xvii acknowledgments xix about this book xxi about the cover illustration xxv PART 1INTRODUCING DART.............................................1 Hello Dart 3 1 1.1 What is Dart? 3 A familiar syntax to help language adoption 5 ■ Single-page application architecture 6 1.2 A look at the Dart language 7 String interpolation 7 ■ Optional types in action 9 Traditional class-based structure 10 ■ Implied interface definitions 11 ■ Factory constructors to provide default implementations 12 ■ Libraries and scope 13 ■ Functions as first-class objects 16 ■ Concurrency with isolates 17 1.3 Web programming with Dart 18 dart:html: a cleaner DOM library for the browser 18 ■ Dart and HTML5 19 vii viii CONTENTS 1.4 The Dart tool ecosystem 20 The Dart Editor 20 ■ Dart virtual machine 21 ■ Dartium 21 dart2js: the Dart-to-JavaScript converter 22 ■ Pub for package management 22 1.5 Summary 23 “Hello World” with Dart tools 24 2 2.1 The command-line Dart VM 25 2.2 “Hello World” with the Dart Editor 26 Exploring the Dart Editor tools 27 ■ The relationship between Dart and HTML files 30 ■ Running “Hello World” with Dartium 30 Using dart2js to convert to JavaScript 32 ■ Generating documentation with dartdoc 34 ■ Debugging Dart with breakpoints 34 2.3 Importing libraries to update the browser UI 35 Importing Dart libraries 36 ■ Accessing DOM elements with dart:html 37 ■ Dynamically adding new elements to the page 38 2.4 Summary 39 Building and testing your own Dart app 40 3 3.1 Building a UI with dart:html 41 Entry-point HTML 42 ■ Creating dart:html elements 42 Creating a new Element from HTML snippets 44 ■ Creating elements by tag name 45 ■ Adding elements to an HTML document 46 3.2 Building interactivity with browser events 49 Adding the PackList item from a button click 49 ■ Event handling with Dart’s flexible function syntax 50 ■ Responding to dart:html browser events 52 ■ Refactoring the event listener for reuse 53 Querying HTML elements in dart:html 54 3.3 Wrapping structure and functionality with classes 56 Dart classes are familiar 57 ■ Constructing the PackItem class 57 ■ Wrapping functionality with property getters and setters 59 3.4 Unit-testing the code 62 Creating unit tests 64 ■ Defining test expectations 64 Creating a custom matcher 66 3.5 Summary 67 CONTENTS ix PART 2CORE DART ......................................................69 Functional first-class functions and closures 71 4 4.1 Examining Dart functions 72 Function return types and the return keyword 74 ■ Providing input with function parameters 77 4.2 Using first-class functions 82 Local function declarations 83 ■ Defining strong function types 88 4.3 Closures 91 4.4 Summary 93 Understanding libraries and privacy 94 5 5.1 Defining and importing libraries in your code 95 Defining a library with the library keyword 96 ■ Importing libraries with import 98 5.2 Hiding functionality with library privacy 103 Using privacy in classes 105 ■ Using private functions in libraries 109 5.3 Organizing library source code 110 Using the part and part of keywords 111 5.4 Packaging your libraries 114 5.5 Scripts are runnable libraries 116 5.6 Summary 118 Constructing classes and interfaces 119 6 6.1 Defining a simple class 120 Coding against a class’s interface 121 ■ Formalizing interfaces with explicit interface definitions 123 ■ Using multiple interfaces 124 ■ Declaring property getters and setters 125 6.2 Constructing classes and interfaces 126 Constructing class instances 127 ■ Designing and using classes with multiple constructors 128 ■ Using factory constructors to create instances of abstract classes 129 ■ Reusing objects with factory constructors 130 ■ Using static methods and properties with factory constructors 132 x CONTENTS 6.3 Creating constant classes with final, unchanging variables 134 Final values and properties 134 ■ The constructor initialization block 134 ■ Using the const keyword to create a const constructor 135 6.4 Summary 136 Extending classes and interfaces 138 7 7.1 Extending classes with inheritance 139 Class inheritance 140 ■ Inheriting constructors 142 Overriding methods and properties 143 ■ Including abstract classes in a class hierarchy 144 7.2 Everything is an object 147 Testing the “is-an” relationship with Object 147 ■ Using the “is- an” Object relationship 149 ■ Using toString() functionality inherited from the base Object class 150 ■ Intercepting noSuchMethod() calls 151 ■ Other default functionality of the Object class 153 7.3 Introducing the dynamic type 154 Using the dynamic type annotation 156 7.4 Summary 156 Collections of richer classes 158 8 8.1 Working with collections of data 159 Collections of objects 160 ■ Using the concrete implementations of the Collection interface 164 ■ Making collections specific with generics 166 ■ Storing lists of key/value pairs with generic maps 170 8.2 Building your own generic classes 173 Defining a generic class 173 ■ Using your custom generic class 175 ■ Restricting the types that can be used as placeholders 175 8.3 Operator overloading 176 Overloading comparison operators 177 ■ Surprising use for operator overloading 179 ■ Overloading indexer operators 179 8.4 Summary 182 Asynchronous programming with callbacks and futures 183 9 9.1 Why web apps should be asynchronous 185 Modifying your app to be asynchronous 187 CONTENTS xi 9.2 Using callbacks with async programming 190 Adding async callbacks to Dart Lottery 192 ■ Ensuring that all async callbacks are complete before continuing 193 ■ Nesting callbacks to enforce async execution order 195 9.3 Introducing the Future and Completer pair 197 Passing around future values 198 ■ Ordering async calls by chaining futures 199 ■ Waiting for all futures to complete 200 Transforming nonfuture values into futures 202 9.4 Unit-testing async APIs 203 Testing async callback functions 205 ■ Testing future values 205 9.5 Summary 207 PART 3CLIENT-SIDE DART APPS ....................................209 Building a Dart web app 211 10 10.1 A single-page web app design 212 Introducing DartExpense 212 ■ Dart application structure 216 Dart app execution flow 217 10.2 Building a UI with dart:html 220 Understanding the Element interface 220 ■ Element constructors in action 223 ■ Building interaction with views and elements 225 ■ Building a simple generic grid 228 10.3 Handling browser events with dart:html 231 Managing browser event flow 232 ■ Common event types 235 10.4 Summary 236 Navigating offline data 237 11 11.1 Integrating navigation with the browser 239 Using pushState() to add items to the browser history 239 Listening for popState events 241 11.2 Using browser cookies to enhance user experience 243 Storing data in a cookie 244 ■ Reading data from a cookie 245 11.3 Persisting data