Apress dart for absolute beginners

320 345 0
Apress dart for absolute beginners

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewers����������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Foreword�������������������������������������������������������������������������������������������������������������������������� xxv Introduction�������������������������������������������������������������������������������������������������������������������� xxvii ■■Chapter 1: Getting Set Up��������������������������������������������������������������������������������������������������1 ■■Chapter 2: Your First Dart Programs���������������������������������������������������������������������������������7 ■■Chapter 3: Some Programming Fundamentals����������������������������������������������������������������15 ■■Chapter 4: Five Small Programs to Showcase Fundamentals in Dart�����������������������������25 ■■Chapter 5: Functions�������������������������������������������������������������������������������������������������������41 ■■Chapter 6: Data Structures����������������������������������������������������������������������������������������������59 ■■Chapter 7: How Does the Web Work?������������������������������������������������������������������������������75 ■■Chapter 8: Using Dart to Interact with HTML�������������������������������������������������������������������89 ■■Chapter 9: Hangman������������������������������������������������������������������������������������������������������111 ■■Chapter 10: Object-Oriented Programming Fundamentals�������������������������������������������125 ■■Chapter 11: Object-Oriented Design������������������������������������������������������������������������������147 ■■Chapter 12: Advanced Dart Concepts����������������������������������������������������������������������������169 ■■Chapter 13: Testing Your Work��������������������������������������������������������������������������������������183 ■■Chapter 14: Concurrency�����������������������������������������������������������������������������������������������197 ■■Chapter 15: Tools of the Trade���������������������������������������������������������������������������������������209 v ■ Contents at a Glance ■■Chapter 16: Putting It All Together��������������������������������������������������������������������������������223 ■■Chapter 17: Where to Go from Here�������������������������������������������������������������������������������239 ■■Chapter 18: Interview with Dart’s Creators�������������������������������������������������������������������251 ■■Appendix A: Dart Cheat Sheet����������������������������������������������������������������������������������������259 ■■Appendix B: History of Web Programming��������������������������������������������������������������������275 ■■Appendix C: Dart Timeline���������������������������������������������������������������������������������������������287 ■■Appendix D: Great Resources����������������������������������������������������������������������������������������289 Index���������������������������������������������������������������������������������������������������������������������������������295 vi Introduction Dart is not just a new language, it’s a new platform for modern web development It’s a superb place to start learning to program, with an eye toward the future of the Web In recent years, the idea that everyone should “learn to code” has become a popular notion With this book, and Dart, you have everything that you will need to “learn to code” with cutting-edge tools The goal of this book is to make learning to program as straightforward as possible Who Is This Book For? Creating a web site is not difficult There are countless tools, both free and commercial, that enable individuals to quickly and easily get their desired content onto the Web For most people, and most cases, those tools are enough This book is not just about creating web sites; it’s about taking one’s first steps toward being able to understand and use the technologies behind the Web This book is aimed at those with a very limited knowledge of web technologies who wish to learn them from the ground up More than anything else, though, this book is an introduction to programming If you are already an experienced programmer, this book is not for you If you know a little HTML but want to take your skillset to the next level, this book is probably for you If you simply have an intense curiosity about computers and the Web but have no knowledge of how they work, this book may be for you If you want to get started developing your own web-based programs with Dart but have no prior experience, then this book is definitely for you No prior knowledge of HTML, CSS, Dart, or even the fundamentals of programming is assumed Programming is not easy The reader of this book cannot expect to have mastered programming by its end She can expect to be capable of writing her own small-to-medium-size programs She will be able to understand others’ source code She will have a firm grasp on the fundamental knowledge and skills necessary to continue her journey In short, she will be on her way to being a programmer Why Should I Learn Dart? Fundamental to web development is the use of a programming language Dart is a new, relatively easy-to-learn programming language from Google The Web has now existed for more than two decades JavaScript, the main client-side programming language used on the Web, is regarded by many to contain some fundamental flaws that have persisted through several revisions of the language Dart was developed as a fresh start One of the most problematic issues with JavaScript is its tendency toward sloppy, hard-to-maintain code Many would point out that this is largely the result of the programmers using JavaScript rather than any fundamental issue with the language itself Yet, because such a wide swath of developers use web languages, differing in background and experience, it makes sense to utilize a language such as Dart, which enforces a structured design for programs from the get-go Of course, a sloppy program can be written in any language—Dart just makes it a little harder xxvii ■ Introduction JavaScript, although object-oriented, does not take the concept of a class hierarchy and inheritance to heart These concepts make programming large projects easier and are at the core of Dart Chapters 10 and 11 fully explore these concepts As of the writing of this book, the latest statistics show native Dart to be approximately two times faster than JavaScript Yet, Dart still can be compiled back to JavaScript (think of this for now as “converted to JavaScript”), so that any modern web browser can run programs written in Dart Dart has a large built-in library (think of this as a real library: you can borrow code from this virtual library to help you things with your program, just as you can borrow books from the real library to help you things in real life) that makes doing many common programming tasks easier Dart also has a powerful packaging system, so that libraries built by other institutions and programmers can easily be utilized by anyone In short, Dart is a modern, convenient, and compatible language designed for productivity It may very well be the future of the Web About This Book This book is structured in short, easy-to-digest chapters that concentrate on one key concept each The idea is to make the book pleasant to read and not intimidating for a first-time programmer The chapters are designed to be read in order, with each building on the previous Some of the chapters contain no code, while others are very heavy with code Both code-heavy and code-light chapters are equally important, if you intend to get the most out of this book This book does not include extensive hands-on lessons regarding how to use Dart Editor, a web browser, or any other tools, beyond the language itself The respective web sites for such pieces of software are better resources for learning about them than any book can be Terms that are likely new to a beginning programmer are italicized and defined in-text or in footnotes The chapters are filled with examples that you are meant to type into your computer line-by-line (no copy-andpasting, please!) Dart for Absolute Beginners takes the philosophy of “learning by doing.” While your hand may be held, you will not be babied The idea is to make the reader accustomed to what real Dart code may look like before she necessarily understands all of its intricacies Do not be intimidated! Keep following along Each chapter ends with exercises to give you more practice with the chapter’s concepts The more you do, the more the chapter’s concepts will begin to seem clear Again, it is recommended that you type the source code from this book into your computer yourself (rather than download it from the Web) It has been speculated in other beginners’ programming books that doing so will help you learn The author of this book agrees A Note on Scope It is not the goal of Dart for Absolute Beginners to exhaustively cover every feature of the Dart language Instead, as an introductory text, it is the goal of this book to make you comfortable with the majority of the language’s features The details are left for further study With that said, the vast majority of the language is covered The topics left out are elaborated in Chapter 17 Conventions Used in This Book Source code throughout this book is formatted in the same way (in terms of bold and italic) in which it would appear in Dart Editor The appearance of an ellipsis (…) indicates that some code is left out, so as not to be too verbose Code is only left out of examples that you are not expected to run All examples that have been designed to be run are complete xxviii ■ Introduction A Note on Dart Versions During the writing of this book, Dart evolved from version 1.0 to version 1.5 All of the source code contained within the book should run correctly on any of those Dart releases The Dart standard library, and certainly the language specification, have largely stabilized It is likely that the source code within this book will continue to be compatible with Dart releases for the foreseeable future, but Dart is a fluid and evolving platform Updated source code, should any incompatibilities arise, will be available online through GitHub, as soon as possible Online Source Code Repository All of the book’s source code at its time of publication is available online on its Apress product page at www.apress.com/9781430264811 You can also get the latest source code on GitHub at https://github.com/davecom/Dart-for-Absolute-Beginners Get in Touch with the Author Do you have questions or comments? Do you need help working through an example or exercise? Please, not hesitate to reach out on Twitter to @davekopec or by e-mail at dartbook@oaksnow.com xxix Chapter Getting Set Up It is sometimes said, “showing up is half the battle.” If that phrase were to be applied to learning how to program a computer, then successfully completing this chapter would be analogous to “showing up.” Once you have your Dart programming environment set up and are comfortable using its basic functions, you may find the rest of this book as easy as copying text from book to screen You will then evolve from our first phrase to another apt one—“learning by doing.” Getting the Tools Dart is a new language, and its tools are still evolving Currently, the tools developed by the authors of the language (Google Inc.) are ubiquitous among Dart developers This situation will likely evolve quickly as adoption of the language accelerates For now, it makes deciding on a virtual work environment relatively easy Happily, these still-evolving tools are free, easy to use, and multiplatform To get started, you will need a fairly modern computer (anything from the last decade should work) running Mac OS X, Windows, or Linux, an Internet connection, and the ability to follow directions Setting up your computer to develop Dart programs is remarkably simple, thanks to the intuitive and easy-to-navigate home of the language on the Web, www.dartlang.org Head over to the Dart web site in a web browser of your choice and download Dart Editor Instead of duplicating the Dart web site’s installation instructions here, you can read the most up-to-date installation instructions for your operating system1 on the official site Go ahead and install Dart Editor The Dart Editor bundles several components you should be aware of • Dart Editor: This is the program you will be using to organize your Dart projects and write the actual source code It will automatically update itself and the other components that are bundled with it so that you are always up to date • Dartium: A version of the Google-sponsored open-source web browser Chromium that is specially tailored to run Dart programs at maximum speed by including a built-in Dart Virtual Machine The low-level software that forms a layer between application programs and computer hardware It provides services for applications to interact both with the hardware and with each other It also provides basic building blocks that make developing new applications easier Well-known personal computer operating systems include OS X, Windows, and Linux The most popular mobile operating systems are Android and iOS 1 Chapter ■ Getting Set Up • Dart Virtual Machine 2 (VM): This is the environment that runs your Dart program More information about Dart Virtual Machine is available in Chapter • Dart2JS: A program that can convert your Dart programs into JavaScript,3 so that they will run on any web browser You will only interact directly with Dart Editor and Dartium The Dart Virtual Machine and Dart2JS work behind the scenes to execute your programs Dart Editor is used to write Dart programs Dartium is used to run them When Dart programs are run in web browsers without the Dart Virtual Machine, they first have to be translated into JavaScript Dartium is the fastest way to test and debug your Dart programs ■■Note  You could write Dart programs in any text editor and use the command line Dart tools to run them Dart Editor is just easier to use There are other Dart development environments available, but they are beyond the scope of this book Using Dart Editor Dart Editor is what is known as an integrated development environment4 (IDE) It may be intimidating when you first open it, but in reality it is fairly easy to use For now, play around with Dart Editor, the help menu, and the instructions on www.dartlang.org to learn how to the following five things (this will also help you get familiar with using these resources): • Creating a new project (or “Application” in Dart Editor’s lingo) • Editing files in the project • Saving files in the project • Adding new files to the project • Running programs Dart Editor is bundled with several example programs Use them to work on figuring out how to perform all of these actions You should try running each of the example programs in Dartium by clicking the run button (it looks like a DVD player’s play button) in Dart Editor Check them out; they’re fun! You will quickly find that using an IDE is not unlike using a word processor The File menu is your starting point for the first four tasks bulleted above, and a colorful icon with a “play” button is used for running your program Once again: This book is not a tutorial on how to use Dart Editor At the time of writing, Dart Editor is still evolving, perhaps even faster than the Dart language itself Therefore, a written tutorial about it would make this book quickly obsolete There are superb resources on the Dart web site and within Dart Editor for learning how to use it But, perhaps the best way to learn a program like Dart Editor is to just explore it on your own When you start a new application in Dart Editor (File ➤ New Application from the menu bar), you have a choice of different application types We will typically be using “Command Line” or “Web Application.” A piece of software that emulates (pretends to be) hardware in order to execute programs written for a hardware-independent architecture Invented by Netscape in 1995, JavaScript is a standardized programming language available on all modern web browsers It is the main client-side programming language of the Web For more information about JavaScript, check out Appendix B A type of software used for developing other software At minimum, it includes a module for editing source code, integration with a compiler, and integration with a debugger 2 Chapter ■ Getting Set Up THE RELATIONSHIP BETWEEN GOOGLE AND DART Google Inc is the 800-lb gorilla behind a lot of the World Wide Web’s emerging technologies, including Dart Google’s large investment in research and development has led to the emergence of two widely publicized programming languages over the past few years The first was Go, a general-purpose systems programming language Dart, a more uniquely web-focused language, is the second As the creator of some of the most widely used web-apps on the planet, Google’s quest to improve the state of web programming is, of course, largely self-serving Google began utilizing Dart on internal, consumer-facing projects long before its 1.0 release While Dart is presently developed largely by Google-employed developers, Google is in the process of standardizing the language with an international standards body JavaScript, the language Dart aims to challenge, was similarly first developed by Netscape Corporation before being standardized as ECMAScript by a standards body (Ecma International, in that case) Google’s efforts to shape the future of web programming and challenge JavaScript have occurred in parallel to those of Microsoft Corporation and its TypeScript language For more information about Dart’s history, check out Appendix C Choosing a Suitable Work Environment It’s a good idea to put all of your Dart documents in one well-organized folder on your computer You will find over and over again that being well-organized makes software development an easier task You should bookmark useful Dart resources in one accessible bookmarks folder in your web browser Having reference material at your fingertips will save you a lot of time when programming Be sure to bookmark some of the resources in Appendix D Programming is a mentally intense task It’s important to have a quiet, comfortable place from which to work Being ergonomic (having a comfortable keyboard, mouse, chair, and monitor setup) really is important, despite whatever jokes you may have heard about it Having an ergonomic work environment is also conducive toward more enjoyment of programming Some programmers find music soothing while writing code, but others find it distracting Generally, the more distracted you are, the worse code you will write, and the more difficult it will be to write This may seem obvious, but when you struggle with a program, take a minute to step away from your computer You may find that returning to the problem after a short while away will provide you with some new clarity when you take a second crack at figuring it out ■■Tip Remember to take breaks! You will enjoy programming more if learning it does not become a chore Try to remind yourself every two hours to spend some time away from the keyboard How to Read This Book The chapters of this book are meant to be approachable in one sitting They are not especially long, but that does not mean you will necessarily pick up the concepts in each quickly The chapters are written in an order that builds conceptually from one to the next For example, it would probably not be a good idea to read Chapter before Chapter 3, because Chapter is composed of several example programs, which demonstrate the concepts from Chapter Therefore, the chapters are meant to be read sequentially Contents About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewers����������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Foreword�������������������������������������������������������������������������������������������������������������������������� xxv Introduction�������������������������������������������������������������������������������������������������������������������� xxvii ■■Chapter 1: Getting Set Up��������������������������������������������������������������������������������������������������1 Getting the Tools����������������������������������������������������������������������������������������������������������������������������1 Using Dart Editor���������������������������������������������������������������������������������������������������������������������������2 Choosing a Suitable Work Environment����������������������������������������������������������������������������������������3 How to Read This Book�����������������������������������������������������������������������������������������������������������������3 Utilizing the Web As You Learn������������������������������������������������������������������������������������������������������4 Search Engines������������������������������������������������������������������������������������������������������������������������������������������������������ Stack Overflow������������������������������������������������������������������������������������������������������������������������������������������������������ Official Dart Sources���������������������������������������������������������������������������������������������������������������������������������������������� Social Media���������������������������������������������������������������������������������������������������������������������������������������������������������� Dart for Absolute Beginners Web Site�������������������������������������������������������������������������������������������������������������������� Summary���������������������������������������������������������������������������������������������������������������������������������������5 ■■Chapter 2: Your First Dart Programs���������������������������������������������������������������������������������7 Hello, World!����������������������������������������������������������������������������������������������������������������������������������7 A Fancier Example�������������������������������������������������������������������������������������������������������������������������9 Your First HTML Document���������������������������������������������������������������������������������������������������������������������������������� 10 Hello World Fancy in Dart������������������������������������������������������������������������������������������������������������������������������������ 12 vii ■ Contents Input and Output��������������������������������������������������������������������������������������������������������������������������13 The Learning Curve���������������������������������������������������������������������������������������������������������������������14 Summary�������������������������������������������������������������������������������������������������������������������������������������14 ■■Chapter 3: Some Programming Fundamentals����������������������������������������������������������������15 Code As Instructions��������������������������������������������������������������������������������������������������������������������15 Variables�������������������������������������������������������������������������������������������������������������������������������������������������������������� 15 Operators������������������������������������������������������������������������������������������������������������������������������������������������������������� 17 Strings����������������������������������������������������������������������������������������������������������������������������������������������������������������� 19 Control Structures����������������������������������������������������������������������������������������������������������������������������������������������� 19 Loops������������������������������������������������������������������������������������������������������������������������������������������������������������������� 22 Summary�������������������������������������������������������������������������������������������������������������������������������������24 ■■Chapter 4: Five Small Programs to Showcase Fundamentals in Dart�����������������������������25 Number Guessing Game��������������������������������������������������������������������������������������������������������������25 Temperature Converter����������������������������������������������������������������������������������������������������������������26 The Monty Hall Problem��������������������������������������������������������������������������������������������������������������30 Pi Calculator��������������������������������������������������������������������������������������������������������������������������������34 Math Test�������������������������������������������������������������������������������������������������������������������������������������36 Summary�������������������������������������������������������������������������������������������������������������������������������������39 ■■Chapter 5: Functions�������������������������������������������������������������������������������������������������������41 What Is a Function?���������������������������������������������������������������������������������������������������������������������41 Function Parameters�������������������������������������������������������������������������������������������������������������������42 The Locality of Variables�������������������������������������������������������������������������������������������������������������������������������������� 42 Multiple Parameters�������������������������������������������������������������������������������������������������������������������������������������������� 44 Function Return Values���������������������������������������������������������������������������������������������������������������44 Single-Line Functions�����������������������������������������������������������������������������������������������������������������46 Rock-Paper-Scissors�������������������������������������������������������������������������������������������������������������������46 Optional Parameters��������������������������������������������������������������������������������������������������������������������49 Positional Optional Parameters��������������������������������������������������������������������������������������������������������������������������� 49 Named Optional Parameters�������������������������������������������������������������������������������������������������������������������������������� 50 viii ■ Contents Functions as First-Class Citizens������������������������������������������������������������������������������������������������51 Functions Within Functions���������������������������������������������������������������������������������������������������������52 Recursive Functions��������������������������������������������������������������������������������������������������������������������52 Fibonacci Sequence�������������������������������������������������������������������������������������������������������������������������������������������� 54 Factorial��������������������������������������������������������������������������������������������������������������������������������������������������������������� 55 Summary�������������������������������������������������������������������������������������������������������������������������������������56 ■■Chapter 6: Data Structures����������������������������������������������������������������������������������������������59 Lists���������������������������������������������������������������������������������������������������������������������������������������������59 List Syntax����������������������������������������������������������������������������������������������������������������������������������������������������������� 60 Birthday Paradox������������������������������������������������������������������������������������������������������������������������������������������������� 61 Simple Blackjack������������������������������������������������������������������������������������������������������������������������������������������������� 63 Maps�������������������������������������������������������������������������������������������������������������������������������������������67 Map Syntax���������������������������������������������������������������������������������������������������������������������������������������������������������� 68 Caesar Cipher������������������������������������������������������������������������������������������������������������������������������������������������������ 68 Baseball Statistics Exercise��������������������������������������������������������������������������������������������������������������������������������� 70 Sets���������������������������������������������������������������������������������������������������������������������������������������������70 Revised containsDuplicates( )������������������������������������������������������������������������������������������������������������������������������ 71 Great Set Use Cases�������������������������������������������������������������������������������������������������������������������������������������������� 71 Summary�������������������������������������������������������������������������������������������������������������������������������������72 ■■Chapter 7: How Does the Web Work?������������������������������������������������������������������������������75 Retrieving a Web Site������������������������������������������������������������������������������������������������������������������75 The Web’s Place on the Internet��������������������������������������������������������������������������������������������������77 Defining a Web Site’s Look����������������������������������������������������������������������������������������������������������79 Non-HTML on the Web����������������������������������������������������������������������������������������������������������������80 Web Browsers�����������������������������������������������������������������������������������������������������������������������������80 Domains, IP Addresses, and IP Routing���������������������������������������������������������������������������������������81 A Little More HTTP�����������������������������������������������������������������������������������������������������������������������83 A Full Web Transaction����������������������������������������������������������������������������������������������������������������86 Summary�������������������������������������������������������������������������������������������������������������������������������������87 ix ■ Contents ■■Chapter 8: Using Dart to Interact with HTML�������������������������������������������������������������������89 The DOM��������������������������������������������������������������������������������������������������������������������������������������89 Tagging the Tags�������������������������������������������������������������������������������������������������������������������������92 Responding to Events from the DOM������������������������������������������������������������������������������������������93 BMI Calculator����������������������������������������������������������������������������������������������������������������������������������������������������� 93 Images�����������������������������������������������������������������������������������������������������������������������������������������97 Memory Game����������������������������������������������������������������������������������������������������������������������������������������������������� 97 Drawing with an HTML Canvas�������������������������������������������������������������������������������������������������103 HTML Canvas Basics����������������������������������������������������������������������������������������������������������������������������������������� 104 Flying Pigs��������������������������������������������������������������������������������������������������������������������������������������������������������� 105 Summary�����������������������������������������������������������������������������������������������������������������������������������109 ■■Chapter 9: Hangman������������������������������������������������������������������������������������������������������111 Word Scramble��������������������������������������������������������������������������������������������������������������������������111 The HTML����������������������������������������������������������������������������������������������������������������������������������������������������������� 111 The Dart������������������������������������������������������������������������������������������������������������������������������������������������������������� 112 Hangman�����������������������������������������������������������������������������������������������������������������������������������113 The Game Resources����������������������������������������������������������������������������������������������������������������������������������������� 114 The HTML and CSS�������������������������������������������������������������������������������������������������������������������������������������������� 115 The Dart������������������������������������������������������������������������������������������������������������������������������������������������������������� 116 Halftime Review������������������������������������������������������������������������������������������������������������������������121 Chapter 1: “Getting Set Up”������������������������������������������������������������������������������������������������������������������������������� 121 Chapter 2: “Your First Dart Programs”��������������������������������������������������������������������������������������������������������������� 121 Chapter 3: “Some Programming Fundamentals”���������������������������������������������������������������������������������������������� 121 Chapter 4: “Five Small Programs to Showcase Fundamentals in Dart”������������������������������������������������������������ 121 Chapter 5: “Functions”�������������������������������������������������������������������������������������������������������������������������������������� 121 Chapter 6: “Data Structures”����������������������������������������������������������������������������������������������������������������������������� 122 Chapter 7: “How Does the Web Work?”������������������������������������������������������������������������������������������������������������� 122 Chapter 8: “Using Dart to Interact with HTML”�������������������������������������������������������������������������������������������������� 122 Looking Forward�����������������������������������������������������������������������������������������������������������������������122 Summary�����������������������������������������������������������������������������������������������������������������������������������123 x ■ Contents ■■Chapter 10: Object-Oriented Programming Fundamentals�������������������������������������������125 What Is an Object?��������������������������������������������������������������������������������������������������������������������125 Object Basics����������������������������������������������������������������������������������������������������������������������������������������������������� 125 References to Objects and Instances���������������������������������������������������������������������������������������������������������������� 126 Defining Classes������������������������������������������������������������������������������������������������������������������������127 Instance Variables��������������������������������������������������������������������������������������������������������������������������������������������� 128 Getters and Setters�������������������������������������������������������������������������������������������������������������������������������������������� 129 Methods������������������������������������������������������������������������������������������������������������������������������������������������������������� 130 Constructors������������������������������������������������������������������������������������������������������������������������������������������������������ 131 Pig��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 133 Class Variables and Class Methods������������������������������������������������������������������������������������������������������������������� 137 The Game of Life�����������������������������������������������������������������������������������������������������������������������138 Summary�����������������������������������������������������������������������������������������������������������������������������������145 ■■Chapter 11: Object-Oriented Design������������������������������������������������������������������������������147 Inheritance��������������������������������������������������������������������������������������������������������������������������������147 Abstract Classes������������������������������������������������������������������������������������������������������������������������150 Geometry Test���������������������������������������������������������������������������������������������������������������������������������������������������� 150 Super�����������������������������������������������������������������������������������������������������������������������������������������154 Interfaces����������������������������������������������������������������������������������������������������������������������������������154 Interfaces Exposed�������������������������������������������������������������������������������������������������������������������������������������������� 155 Casting��������������������������������������������������������������������������������������������������������������������������������������������������������������� 156 Mixins����������������������������������������������������������������������������������������������������������������������������������������156 The Cascade Operator���������������������������������������������������������������������������������������������������������������157 Alien Invaders����������������������������������������������������������������������������������������������������������������������������158 Summary�����������������������������������������������������������������������������������������������������������������������������������167 ■■Chapter 12: Advanced Dart Concepts����������������������������������������������������������������������������169 Operator Overloading����������������������������������������������������������������������������������������������������������������169 The Point Class�������������������������������������������������������������������������������������������������������������������������������������������������� 169 Overloading Operators in Your Own Classes������������������������������������������������������������������������������������������������������ 170 Getting Back to the Point����������������������������������������������������������������������������������������������������������������������������������� 171 xi ■ Contents Generics������������������������������������������������������������������������������������������������������������������������������������171 Exceptions���������������������������������������������������������������������������������������������������������������������������������173 Working with Dart’s Built-in Exceptions������������������������������������������������������������������������������������������������������������ 173 Defining Your Own Exceptions��������������������������������������������������������������������������������������������������������������������������� 174 Factory Constructors�����������������������������������������������������������������������������������������������������������������176 Assert����������������������������������������������������������������������������������������������������������������������������������������178 Typedef��������������������������������������������������������������������������������������������������������������������������������������179 Metadata�����������������������������������������������������������������������������������������������������������������������������������180 Dart’s Built-in Annotations��������������������������������������������������������������������������������������������������������������������������������� 180 Defining Your Own Annotations������������������������������������������������������������������������������������������������������������������������� 181 Summary�����������������������������������������������������������������������������������������������������������������������������������182 ■■Chapter 13: Testing Your Work��������������������������������������������������������������������������������������183 Including External Packages in Your Program���������������������������������������������������������������������������183 Unit Testing��������������������������������������������������������������������������������������������������������������������������������184 Taking a unittest������������������������������������������������������������������������������������������������������������������������������������������������ 184 Grouping Unit Tests�������������������������������������������������������������������������������������������������������������������������������������������� 186 Tic-Tac-Toe��������������������������������������������������������������������������������������������������������������������������������187 Defining the Game��������������������������������������������������������������������������������������������������������������������������������������������� 188 Testing Tic-Tac-Toe�������������������������������������������������������������������������������������������������������������������������������������������� 191 Beta Testing�������������������������������������������������������������������������������������������������������������������������������194 Usability Testing������������������������������������������������������������������������������������������������������������������������195 Summary�����������������������������������������������������������������������������������������������������������������������������������196 ■■Chapter 14: Concurrency�����������������������������������������������������������������������������������������������197 The Hardware Impetus for Parallelism��������������������������������������������������������������������������������������197 Futures��������������������������������������������������������������������������������������������������������������������������������������198 Using Futures with HttpRequest������������������������������������������������������������������������������������������������������������������������ 198 Using Futures with File�������������������������������������������������������������������������������������������������������������������������������������� 199 Stringing Futures Together�������������������������������������������������������������������������������������������������������������������������������� 200 xii ■ Contents Isolates��������������������������������������������������������������������������������������������������������������������������������������200 Starting Up an Isolate���������������������������������������������������������������������������������������������������������������������������������������� 200 Communicating Between Isolates��������������������������������������������������������������������������������������������������������������������� 201 The Dining Philosopher�������������������������������������������������������������������������������������������������������������������������������������� 203 Summary�����������������������������������������������������������������������������������������������������������������������������������208 ■■Chapter 15: Tools of the Trade���������������������������������������������������������������������������������������209 Git����������������������������������������������������������������������������������������������������������������������������������������������209 Git in a Nutshell������������������������������������������������������������������������������������������������������������������������������������������������� 209 Setting Up a Remote Repository������������������������������������������������������������������������������������������������������������������������ 210 Committing Changes to the Repository������������������������������������������������������������������������������������������������������������� 213 Pulling Changes������������������������������������������������������������������������������������������������������������������������������������������������� 216 Debugging Dart with Breakpoints���������������������������������������������������������������������������������������������218 Incorporating Open-Source Packages���������������������������������������������������������������������������������������220 API Documentation��������������������������������������������������������������������������������������������������������������������221 Summary�����������������������������������������������������������������������������������������������������������������������������������222 ■■Chapter 16: Putting It All Together��������������������������������������������������������������������������������223 Constraint Satisfaction Problems����������������������������������������������������������������������������������������������223 Australian Map Coloring Problem���������������������������������������������������������������������������������������������������������������������� 224 How Is a Constraint Satisfaction Problem Solved?�������������������������������������������������������������������������������������������� 226 The constraineD Library������������������������������������������������������������������������������������������������������������������������������������ 226 Solving the Australian Map Coloring Problem Using constraineD��������������������������������������������������������������������� 227 Word Search������������������������������������������������������������������������������������������������������������������������������227 Getting Started�������������������������������������������������������������������������������������������������������������������������������������������������� 228 Search Words����������������������������������������������������������������������������������������������������������������������������������������������������� 229 Facing Constraints��������������������������������������������������������������������������������������������������������������������������������������������� 231 Defining a Grid��������������������������������������������������������������������������������������������������������������������������������������������������� 232 The Glue������������������������������������������������������������������������������������������������������������������������������������������������������������� 234 You’re Not Done Yet������������������������������������������������������������������������������������������������������������������������������������������� 238 Summary�����������������������������������������������������������������������������������������������������������������������������������238 xiii ■ Contents ■■Chapter 17: Where to Go from Here�������������������������������������������������������������������������������239 Advance Your Core Dart Skills���������������������������������������������������������������������������������������������������239 Project Ideas�����������������������������������������������������������������������������������������������������������������������������240 Learn Polymer.dart and/or Angular.dart������������������������������������������������������������������������������������240 Server-Side Dart������������������������������������������������������������������������������������������������������������������������241 Working with Databases�����������������������������������������������������������������������������������������������������������241 Learn Computer Science�����������������������������������������������������������������������������������������������������������242 What Is Computer Science?������������������������������������������������������������������������������������������������������������������������������ 242 Examples of Computer Science Problems in This Book������������������������������������������������������������������������������������ 244 Learn Another Language�����������������������������������������������������������������������������������������������������������244 Python���������������������������������������������������������������������������������������������������������������������������������������������������������������� 245 JavaScript���������������������������������������������������������������������������������������������������������������������������������������������������������� 245 C������������������������������������������������������������������������������������������������������������������������������������������������������������������������ 246 Scheme�������������������������������������������������������������������������������������������������������������������������������������������������������������� 247 Learn How to Set Up a Web Server�������������������������������������������������������������������������������������������247 Learn Web Design���������������������������������������������������������������������������������������������������������������������248 Learn a WYSIWYG Web Design Tool������������������������������������������������������������������������������������������������������������������� 248 Learn a Graphics Package��������������������������������������������������������������������������������������������������������������������������������� 248 Learn a CSS Framework������������������������������������������������������������������������������������������������������������������������������������ 249 Get Involved with the Dart Community��������������������������������������������������������������������������������������249 Get in Touch with the Author�����������������������������������������������������������������������������������������������������250 Summary�����������������������������������������������������������������������������������������������������������������������������������250 ■■Chapter 18: Interview with Dart’s Creators�������������������������������������������������������������������251 Interview with Lars Bak and Kasper Lund��������������������������������������������������������������������������������251 Part Dart’s Formulation and Intent���������������������������������������������������������������������������������������������������������������� 251 Part Dart As a First Programming Language ������������������������������������������������������������������������������������������������ 254 xiv ■ Contents ■■Appendix A: Dart Cheat Sheet����������������������������������������������������������������������������������������259 The Basics���������������������������������������������������������������������������������������������������������������������������������259 Declaring and Initializing Variables (Chapter 3)������������������������������������������������������������������������������������������������� 259 Literals (Chapter 3, Chapter 6)��������������������������������������������������������������������������������������������������������������������������� 260 Common Operators�������������������������������������������������������������������������������������������������������������������������������������������� 260 Control Structures (Chapter 3)��������������������������������������������������������������������������������������������������������������������������� 262 Loops (Chapter 3, Chapter 6)����������������������������������������������������������������������������������������������������������������������������� 262 Numbers������������������������������������������������������������������������������������������������������������������������������������������������������������ 263 Strings��������������������������������������������������������������������������������������������������������������������������������������������������������������� 263 Constants and Final Variables��������������������������������������������������������������������������������������������������������������������������� 264 Giving Programs Structure��������������������������������������������������������������������������������������������������������264 Functions (Chapter 5)���������������������������������������������������������������������������������������������������������������������������������������� 264 Lists (Chapter 6)������������������������������������������������������������������������������������������������������������������������������������������������ 265 Maps (Chapter 6)����������������������������������������������������������������������������������������������������������������������������������������������� 265 Sets (Chapter 6)������������������������������������������������������������������������������������������������������������������������������������������������� 266 Defining Classes (Chapter 10, Chapter 11)�������������������������������������������������������������������������������������������������������� 266 Libraries (Chapter 10)���������������������������������������������������������������������������������������������������������������������������������������� 268 Key Packages in the Standard Library��������������������������������������������������������������������������������������268 dart:html (Chapter 8)����������������������������������������������������������������������������������������������������������������������������������������� 269 dart:io (Chapter 4, Chapter 14)�������������������������������������������������������������������������������������������������������������������������� 270 dart:math����������������������������������������������������������������������������������������������������������������������������������������������������������� 270 unittest (Chapter 13)������������������������������������������������������������������������������������������������������������������������������������������ 271 dart:async (Chapter 8, Chapter 14)�������������������������������������������������������������������������������������������������������������������� 272 dart:isolate (Chapter 14)������������������������������������������������������������������������������������������������������������������������������������ 273 General Style Conventions��������������������������������������������������������������������������������������������������������273 ■■Appendix B: History of Web Programming��������������������������������������������������������������������275 Client Side���������������������������������������������������������������������������������������������������������������������������������275 Java Applets������������������������������������������������������������������������������������������������������������������������������������������������������ 275 JavaScript���������������������������������������������������������������������������������������������������������������������������������������������������������� 276 VBScript������������������������������������������������������������������������������������������������������������������������������������������������������������� 276 xv ■ Contents Flash������������������������������������������������������������������������������������������������������������������������������������������������������������������ 277 Silverlight���������������������������������������������������������������������������������������������������������������������������������������������������������� 277 Recent Developments���������������������������������������������������������������������������������������������������������������������������������������� 277 Server Side��������������������������������������������������������������������������������������������������������������������������������278 CGI��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 278 Perl�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 279 PHP�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 279 ASP�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 280 Java������������������������������������������������������������������������������������������������������������������������������������������������������������������� 280 Python���������������������������������������������������������������������������������������������������������������������������������������������������������������� 280 Ruby������������������������������������������������������������������������������������������������������������������������������������������������������������������� 281 JavaScript���������������������������������������������������������������������������������������������������������������������������������������������������������� 281 Trends���������������������������������������������������������������������������������������������������������������������������������������������������������������� 282 Where Does Dart Fit In?������������������������������������������������������������������������������������������������������������283 Evolution of the Web Browser���������������������������������������������������������������������������������������������������283 Microsoft and Netscape Duke It Out������������������������������������������������������������������������������������������������������������������ 283 Firefox Emerges from the Ashes of Netscape��������������������������������������������������������������������������������������������������� 284 Mobile and a Revitalized Browser Ecosystem��������������������������������������������������������������������������������������������������� 284 Importance Today����������������������������������������������������������������������������������������������������������������������285 ■■Appendix C: Dart Timeline���������������������������������������������������������������������������������������������287 ■■Appendix D: Great Resources����������������������������������������������������������������������������������������289 Dart�������������������������������������������������������������������������������������������������������������������������������������������289 Books����������������������������������������������������������������������������������������������������������������������������������������������������������������� 289 Web Sites����������������������������������������������������������������������������������������������������������������������������������������������������������� 289 Articles�������������������������������������������������������������������������������������������������������������������������������������������������������������� 290 HTML & CSS������������������������������������������������������������������������������������������������������������������������������292 Books����������������������������������������������������������������������������������������������������������������������������������������������������������������� 292 Web Sites����������������������������������������������������������������������������������������������������������������������������������������������������������� 292 xvi ■ Contents A Second Programming Language �������������������������������������������������������������������������������������������293 Books����������������������������������������������������������������������������������������������������������������������������������������������������������������� 293 Other�����������������������������������������������������������������������������������������������������������������������������������������294 Books����������������������������������������������������������������������������������������������������������������������������������������������������������������� 294 Index���������������������������������������������������������������������������������������������������������������������������������295 xvii About the Author David Kopec, a veteran of two web startups, is a software developer, entrepreneur, and author from New York He is passionate about the use of computer technology for improving human welfare David holds a bachelor’s degree in economics and English and a master’s degree in computer science, both from Dartmouth College Get in touch with him on Twitter @davekopec or by e-mail at dartbook@oaksnow.com xix About the Technical Reviewers Matthew Butler is a software developer and system administrator with Seaside Communications where he uses Dart on a number of inhouse tools He has over 14 years experience in the industry in various roles Matthew has contributed source code and documentation directly to Google’s Dart programming language and has been active in the Dart community since 2012 He spends his evenings at home in Nova Scotia, Canada, with his wife Julie Ann and his boys Cody and Jaxon You can contact him at www.google.com/+MatthewButler David Titarenco is a twenty-something software engineer from sunny Los Angeles With more than a decade of experience, he specializes in Java, JavaScript, Go, and C/C++ back end development but he recently held a number of positions that also familiarized him with front end technologies like jQuery, Ext.js, Backbone, CoffeeScript, and Scala David is a proponent of open source and has contributed to high-profile projects like Google Go and the Java Kilim microthreading library A lover of startups, he founded nearly half a dozen in the past six years and shows no signs of letting up David graduated magna cum laude from UCLA where he studied philosophy and mathematics He sometimes blogs at http://dvt.name and can be reached at david.titarenco@gmail.com xxi Acknowledgments I would like to thank Lars Bak and Kasper Lund of Google, the creators of Dart, for taking time out of their busy schedules to sit down for an interview They provided their time very generously and made themselves available as well for significant follow-up Thanks must also go to another Googler, Seth Ladd, who wrote the foreword to this book Thanks, Seth! There is an increasing trend for technical books to be self-published I knew I wanted a traditional publisher for this book, and Apress was the first (and only) publisher I approached I want to thank Ben Renow-Clarke for believing in Dart for Absolute Beginners from the beginning and guiding the project throughout its life A lot of people don’t realize that when a technical book gets published by a traditional publisher, you’re getting not just the author’s insight but an entire team, including an acquisition editor (Ben Renow-Clarke), copy editor (Michael G Laraque), development editors (Chris Nelson and Douglas Pundick), and coordinating editors (Christine Ricketts and Anamika Panchoo) I would like to thank all of them, as well as many unnamed at Apress and its partners who worked on the cover, layout, etc., to deliver the book that you’re reading today I have to single out Chris Nelson for the level of care and consideration he put into Dart for Absolute Beginners He really helped fill in the creases The technical reviewer, Matthew Butler, was detail-oriented and great at catching my errors Thanks, Matt! I would like to give a special shout-out to professors Devin Balkcom, Tom Cormen, Scot Drysdale (who in addition to teaching me programming languages, gave me tips for my interview with Lars and Kasper), and Gevorg Grigoryan at Dartmouth College Some examples I first saw in their classes, years ago, made it into this book I’d like to thank my family and friends for being supportive of me writing this book I come from a long line of teachers/professors, including my dad, brother, aunt, and grandmother This book is my contribution to that tradition My dad got me started computer programming when I was eight years old and always made sure I had the resources I needed to be good at it My mom more than anyone else supported our family during a very difficult time period that coincided with the writing of this book Thanks, Mom, for making sure everyone was okay! Finally, although trite and cliché, I’d like to thank you, the reader A lot of people today choose to follow a hodgepodge of online tutorials to figure out beginners’ programming They miss out on the experience of having one voice, which knows how much they know at every step of the process, teach them clearly and deliberately Your support of books like this, through your purchase, makes it possible for them to continue to exist in an online tutorial world I think you’ll be a better programmer for it —David Kopec New York May 2014 xxiii Foreword Now is a great time to learn web development The modern Web today is feature-rich, works across multiple devices, and delivers high-fidelity experiences that delight and astound You don’t have to ask anyone’s permission to build something amazing and publish it for the world All the tools you need are available for free Your journey begins with Dart, a programming language that is approachable, easy-to-learn, and powerful Inspired by the good parts of many popular languages, and built to get you up and running quickly, Dart provides the tools you need to build web apps, without the dark corners or alleyways you might find in traditional web development Getting started with Dart means you can avoid the false starts and “old wives tales” of web development, which have built up over years and years of evolution Dart for Absolute Beginners accelerates your web development career Using this book’s mix of practical examples and fundamentals, you will learn how to build apps for the modern Web with a modern language and tools You will also learn how the Web works and gain a better appreciation of the web stack, important context for any new developer I wish I had a tool like Dart when I started web development, but I’m glad you Have fun—and welcome to the Web! —Seth Ladd xxv ... onto it for future use = is used differently in Dart than it is in algebra As has been mentioned before, = is used in Dart for assignment (assigning values to variables) In Dart, == is used for equality... newcomers Dart for Absolute Beginners Web Site All of the source code and projects in this book can be downloaded from the Dart for Absolute Beginners book page at www .apress. com Do not just go... available online on its Apress product page at www .apress. com/9781430264811 You can also get the latest source code on GitHub at https://github.com/davecom /Dart- for- Absolute- Beginners Get in Touch

Ngày đăng: 11/05/2017, 15:42

Mục lục

  • Contents at a Glance

  • About the Technical Reviewers

  • Chapter 1: Getting Set Up

    • Getting the Tools

    • Choosing a Suitable Work Environment

    • How to Read This Book

    • Utilizing the Web As You Learn

      • Search Engines

      • Dart for Absolute Beginners Web Site

      • Chapter 2: Your First Dart Programs

        • Hello, World!

        • A Fancier Example

          • Your First HTML Document

          • Hello World Fancy in Dart

          • Chapter 3: Some Programming Fundamentals

            • Code As Instructions

              • Variables

              • Control Structures

                • More About Booleans

                • Chapter 4: Five Small Programs to Showcase Fundamentals in Dart

                  • Number Guessing Game

                  • The Monty Hall Problem

                  • Chapter 5: Functions

                    • What Is a Function ?

                    • Function Parameters

                      • The Locality of Variables

                      • Optional Parameters

                        • Positional Optional Parameters

                        • Functions as First-Class Citizens

                        • Chapter 6: Data Structures

                          • Lists

                            • List Syntax

                            • Great Set Use Cases

Tài liệu cùng người dùng

Tài liệu liên quan