logo资料库

Web Programming with Dart.pdf

第1页 / 共446页
第2页 / 共446页
第3页 / 共446页
第4页 / 共446页
第5页 / 共446页
第6页 / 共446页
第7页 / 共446页
第8页 / 共446页
资料共446页,剩余部分请下载后查看
Contents at a Glance
Contents
About the Authors
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Dart’s Flightpath So Far
What is Dart?
Advantages to Using Dart
Dart’s Origins
Converting From Other Programming Languages
Dart Names are Simpler and More Structured
Searching for DOM Elements is Easier
Dart Has Real Data Collections
Constructors to Create New DOM Elements
Dart Improves and Unifies the Events System
Say Goodbye to Vendor Prefixes
Future-Based APIs
Libraries
Cross-Browser Consistency
Cascaded DOM Construction
Why Dart is Perfect for Web Development
Summary
Part I: Getting Started
Chapter 2: Setting up Dart Correctly
Downloading and Installing the Software
Setting Up a Previous Version of Dart Editor
Dart Editor and the Software Development Environment
Opening and Running an Example
Creating Your First Application
Creating a Web Application
Creating Command-Line Applications
Summary
Part II: Dart Tools
Chapter 3: Making the Most of the Dart Editor
The Dart Editor in Detail
Opening Some Examples
Creating Applications
Dart Editor Options and Main Views
Preferences and Views
Outline View Additional Buttons
Problems View Additional Buttons
Setting Up the Views
Dart Editor Preferences
Running Applications
How Does this Sample Application Run?
Advanced Examples
Debugging Applications
Inspecting the SDK
Summary
Chapter 4: Using Pub and dart2js to Compile Applications to JavaScript
Reviewing the Compliation Process
Pub build
dart2js
Pub Serve
Summary
Chapter 5: Application Launches
Manage Launches
Dart Command-Line Launch
Dartium Launch
Chrome App Launch
Browser Launch
Android Mobile Launch
Summary
Chapter 6: Using the Dart Pub Package Manager
What is Pub?
Digging Deeper
Working With Commands and Packages
Pub Build
Pub Cache
Pub Deps
Pub Get
Pub Global
Pub Publish
Pub Run
Pub Serve
Pub Upgrade
Pub Uploader
Pub Downgrade
Pub help
Pub version
Additional Pub Commands
Summary
Chapter 7: Optimizing for Dart VM and Dartium
Dart Virtual Machine
Dartium
Checked Mode
Summary
Chapter 8: Cleaning up with Dart Formatter
Dart Formatter
Summary
Chapter 9: Generating Dart Code Documentation
docgen
DocGen Options
Summary
Chapter 10: Monitoring Your Applications with Observatory
Observatory
Observatory and Command-Line Applications
Observatory and Web Applications
Summary
Part III: The Dart Language: Basics
Chapter 11: The Dart Comment System
Comments Overview
Inline Comments
Block Comments
Documentation Comments
Markdown
Links
Block Styles
Inline Styles
Summary
Chapter 12: Understanding Operators and Expressions
An Introduction to Dart Operators
Operator Types
Arithmetic
Equality and Relational
Type Test
Assignment
Conditional
Bit
Others
An Introduction to Expressions
Conditional
Logical
Bit
Equality
Relational
Bitwise
Addition
Multiplication
Pre-Expressions and Post-Expressions
Assignment
How to Create Your Own Operators
Summary
Chapter 13: Mastering Dart’s Variables and Data Types
An Introduction to Variables and Data Types
Variables Definition
Types of Variables: Final and Const
Data Types
Numbers
Working with Numbers in Dart
dart:math Library
Random Numbers
Strings
Working with Strings
Regular Expressions
Brackets
Metacharacters
Booleans
Working with Bool Values
Lists
Working with the List Class
Iterable
Sets
Maps
Date and Time
Summary
Chapter 14: Flow Control Statements
If and Else
Switch Statement
For Loop
While Loop
Summary
Chapter 15: Working with Functions
Defining Functions
Positional Optional Parameters
Named Optional Parameters
Default Values
Return Values
Recursive Functions
Summary
Chapter 16: A Syntax Summary and Cheat Sheet
Part IV: The Dart Language: Advanced
Chapter 17: Processing Exceptions and Error Control
Exceptions and Error Control
Exceptions Types
Error Types
Exceptions and Error Definitions
Summary
Chapter 18: Understanding Dart Classes
Instance Variables
Constructors
Methods
Inheritance
Variables and Static Methods
Summary
Chapter 19: Implicit Interfaces and Abstract Classes
Summary
Chapter 20: Implementing Generics and Typedefs
Generics
Typedef
Summary
Chapter 21: Using Dart’s Libraries
Using Libraries
Creating Libraries with Multiple Files
Re-exporting Libraries
Summary
Chapter 22: Leveraging Isolates for Concurrency and Multi-Processing
Summary
Chapter 23: Asynchronous Programming with Dart
What is Asynchronous Programming?
Future
Working with Future
Stream
Working with Stream
Creating Asynchronous Functions
Summary
Chapter 24: An Advanced Syntax Summary
Exceptions and Errors
Classes
Part V: Dart and Other Web Technologies
Chapter 25: Integrating Dart and HTML5
Working with HTML
Event Management
Summary
Chapter 26: Integrating Dart and CSS
Working with CSS
Visual Effects
FadeIn and FadeOut
Flashing Effect
Expand and Collapse Items
Summary
Part VI: Dart and the Web Server
Chapter 27: Combining Web Services with Dart
Asynchronous HTTP Requests with Dart
Using JSON in Dart
Summary
Chapter 28: Dart on the Server Side
The I/O Dart Library
Creating a Server Application
Handling Requests
Working with Processes
Sockets
Summary
Part VII: Dart’s Future
Chapter 29: Implementing Design Patterns and Creating Web Components
Web Components
Polymer.dart
What is the Shadow DOM?
Working with Polymer.dart
Summary
Chapter 30: Developing a Dart App
Designing the Application
Creating the Application
Coding the Application
Summary
Index
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. www.it-ebooks.info
Contents at a Glance About the Authors ��������������������������������������������������������������������������������������������������������������� xv About the Technical Reviewer ������������������������������������������������������������������������������������������ xvii Acknowledgments ������������������������������������������������������������������������������������������������������������� xix Introduction ����������������������������������������������������������������������������������������������������������������������� xxi ■ Chapter 1: Dart’s Flightpath So Far �����������������������������������������������������������������������������������1 ■ ■ Part I: Getting Started Chapter 2: Setting up Dart Correctly ��������������������������������������������������������������������������������� 13 �������������������������������������������������������������������������������15 ■ ■ Part II: Dart Tools Chapter 3: Making the Most of the Dart Editor ���������������������������������������������������������������������������������������� 31 ����������������������������������������������������������������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 Chapter 11: The Dart Comment System ���������������������������������������������������������� 147 �������������������������������������������������������������������������149 ■ Chapter 12: Understanding Operators and Expressions ������������������������������������������������159 ■ Chapter 13: Mastering Dart’s Variables and Data Types �����������������������������������������������171 www.it-ebooks.info iii
■ Contents at a GlanCe ■ 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 Chapter 17: Processing Exceptions and Error Control ����������������������������������������������������� 223 ��������������������������������������������������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 Dart’s 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 Chapter 25: Integrating Dart and HTML5 ������������������������������������������������� 305 �����������������������������������������������������������������������307 ■ Chapter 26: Integrating Dart and CSS ���������������������������������������������������������������������������321 ■ ■ Part VI: Dart and the Web Server Chapter 27: Combining Web Services with Dart �������������������������������������������������������������� 339 ������������������������������������������������������������341 ■ Chapter 28: Dart on the Server Side ������������������������������������������������������������������������������367 ■ ■ Part VII: Dart’s Future Chapter 29: Implementing Design Patterns and Creating Web Components ������������������������������������������������������������������������������� 387 �����������������389 ■ Chapter 30: Developing a Dart App �������������������������������������������������������������������������������407 Index ���������������������������������������������������������������������������������������������������������������������������������437 iv www.it-ebooks.info
Introduction Welcome to Web Programming with Dart. Early, in our careers we came to recognize how our world would change thanks to the Internet and especially to web applications. Every device we use in our daily lives uses the Internet intensively. If we consider the different sizes and characteristics of devices and the different operating systems available, we understand the complexity of software application development. Web applications can help us with that. You develop an application and deploy it through different devices and screen sizes, but to accomplish this task you need a programming language designed to understand the web development complexity and run in different platforms. The applications must be highly optimized and support numerous options: geo-localization, notifications, upload and play media files, or the interaction between users, among others. We can develop web applications with those functionalities but the only way to develop such complex applications is by using a high-level programming language specific to Web development with support for the latest web technologies. In this book you will master Dart, with a progressive and dynamic learning approach. It is one of the most complete books on Dart written so far and provides the basics of developing web applications using Dart, an object-oriented and structured language that supports interfaces, large hierarchy of classes, and optional typing. You will see examples and even develop your first complete web application, step by step. You will learn how to integrate Dart with CSS3 and HTML 5, combine Web Services with Dart, use Dart on the server side, implement design patterns and create web components. The book had been written with consideration for programmers who want to learn this new programming language but also for web designers because Dart can interoperate perfectly with HTML5 and CSS3. This book is oriented to developers and web designers who want to develop high-level web applications and are sick and tired of fighting with the creation of cross-platform web applications; for everyone who needs a new, yet familiar web programming language for modern web applications and really big projects; and for developers who need a typed language for a fast, secure, and easy development and debugging process. In the book we will travel from the most basic to the most complex functions of this language but we will also cover all the tools available to get the most of Dart. With this book you will know the language, how the tools work, and you will see the core functions and libraries. We will show you the history behind Dart and how it came to us. We will develop some UI animation samples for web, work with web services, server-side Dart applications, and the new Polymer.dart library for the new HTML UI web component generation. • • • We will take a broad tour, showing the Dart basic and advanced functionalities. You will learn the tools that come with Dart SDK and the main, most important libraries. Additionally you will work with the newest Polymer.dart library for web component creation. You will be able to develop your own command-line and server-side applications and, of course, web applications with Dart. www.it-ebooks.info xxi
Chapter 1 Dart’s Flightpath So Far In this chapter we will introduce you the Dart programming language, including how Dart functions and what Dart is. We’ll see what structured programming is and how we can take advantage of it using Dart. Dart brings us great advantages for web development. We will see these advantages compared to JavaScript or jQuery on the client side as well as Dart’s most interesting functionalities. What is Dart? Dart is an open-source, structured, and flexible programming language developed by Google, particularly oriented for web development, but not exclusively. This new programming language has arrived to make programmers lives easy, allowing them to develop more complex web applications with better maintenance and improved performance. Note ■ Structured programming is a programming model aimed at improving the clarity and the quality of the code, cutting down development time, using subroutines and three basic structures: block structures, selection structures (if and switch), and looping structures (for and while loops). Behind Dart are Lars Bak and Kasper Lund, two of the authors of the V8 JavaScript engine for Google Chrome. For that reason the performance and efficiency of the language were two factors that were very important at the moment of developing this language. Gilad Bracha was also involved in the creation of Dart. Bracha is the author of Newspeak Programming Language, co-author of Java Specifications and the second edition of the Java Virtual Machine Development. Dart is a class-based, object-oriented language with simple inheritance. Dart also supports interfaces, abstract classes, and optional typing. Don’t worry about all of those terms. We will see everything in depth step by step and you will enjoy all of those advantages in a very simple way. We have commented that Dart is an alternative for web development, especially in the client side. Currently the trend is that, programmers try to develop most of the tasks in the client side allowing the server to be smaller and faster. Thus, with a very simple, lighter server it can manage more requests per second. Despite what it seems Dart was not developed to replace JavaScript, however it was developed to offer an additional, modern option for web development with better performance and above all for big projects in which the maintenance process is complicated. Dart has a C-style syntax, thereby to all programming languages that inherit from it. So, if you have previously worked with JavaScript, Java, PHP, C++, Objective C, or C#, you would be very happy because you now know Dart. www.it-ebooks.info 1
Chapter 1 ■ Dart’S Flightpath So Far Dart is the best option for really big web projects with special importance in the client side. This programming language allows for better organization of the code and you can maintain your project more easily than with others programming languages, such as JavaScript. Dart produces a very readable code and runs in mainstream browsers (you can compile your Dart code to JavaScript and run it in all of the modern web browsers). Dart code runs over DartVM (Dart Virtual Machine), which is two times faster than JavaScript. As you can see in Figure 1-1, Dart code compiled to JavaScript is faster than native JavaScript code running on V8 engine for the Tracer test. Figure 1-1. Dart VM, dart2js, and JavaScript V8 performance ■ Note You can learn more about Dart performance in https://www.dartlang.org/performance/ 2 www.it-ebooks.info
Chapter 1 ■ Dart’S Flightpath So Far Advantages to Using Dart One of the main advantages is its high performance. Currently, in some benchmarks, even running Dart code compiled to JavaScript is faster than JavaScript native code running on V8. Another advantage is its simplicity and clarity when you use it and most importantly when you have to learn it. Dart offers unimaginable possibilities when you need to develop and maintain big, complex applications. Furthermore, Google Dart developers and the open-source community have created great documentation, including tutorials and samples. This is welcome when you need to start with a new programming language. One great advantage is the ability to use a full IDE (Integrated Development Environment) just out of the box. This IDE is known as Dart Editor where you can find all the SDK, classes, and methods with their documentation, a very powerful debugger, and tools to compile and test your applications. Dart Editor is not the only IDE you can use to work with Dart. Dart has official support for other editors like Intellij / Webstorm, Eclipse, or Sublime Text 2 and 3. Asynchronous programming is one of the most powerful features of this new language. We will dive into this subject in coming chapters but for the moment we want to explain the basic idea of the asynchronous programming in Dart. The asynchronous programming is made in Dart thanks to Future API, which allows you to run tasks and processes in the background without waiting to get the results of these processes. The results will come later and you’ll be able to get those results and make other tasks, change the layout, or show information to the user. We don’t want to end this section without mentioning a very interesting and powerful advantage. You can use Dart to develop web applications, as we previously have said, but in addition, you can use Dart to develop command-line applications and server-side applications. You only need to have DartVM installed on your system to run your Dart applications. You can run your Dart apps on Mac, Windows, and Linux. But not all aspects of Dart are advantages. Regardless of the documentation we have mentioned, you cannot find a lot of documentation as it applies to other programming languages like PHP or JavaScript. Dart is a very young programming language, having just reached its 1.0 stable version at the end of 2013. With the new releases 1.6 and 1.7 everything would be easier. Google and the community are making efforts to improve the Dart documentation and, we also hope to help meet the shortfalls of documentation and make the learning task easy and fun with this book. Dart’s Origins The programming paradigm has changed a lot over the past few years. To start with the server was in charge of doing everything. When a user visits our web page (as shown in Figuserver makes database queries, makes access validatre Figure 1-2), the ion processes, builds the HTML document, and loads external resources as CSS, JavaScript scripts, images, and videos. This is happening in web applications developed with PHP, Python, JSP, or Ruby. Figure 1-2. Standard server-client requests scheme www.it-ebooks.info 3
分享到:
收藏