Book Image

Learning Dart

Book Image

Learning Dart

Overview of this book

Table of Contents (19 chapters)
Learning Dart
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Developing a web application or software in general, is still a challenging task. There is a client/browser side and a server side with databases. There are many different technologies to master in order to feel comfortable with a full client/server stack. There are different frameworks with different objectives. There are also different programming languages that a developer must learn each one more suitable either for the server or the client side.

Learning Dart will help a developer to become more focused by using Dart both for clients and servers. Using the same language will ensure that a developer will lose neither performance nor flexibility. Dart can be used within its virtual machine, or its code may be compiled to JavaScript. In both cases, the performance benchmarks show promising scores (https://www.dartlang.org/performance/). Dart is both an object-oriented and a functional language. With Dart, a mix of both approaches is possible, providing great professional freedom and a programming background flexibility. In addition, Dart provides many libraries and tools (http://pub.dartlang.org/) that allow a developer to focus on the task at hand and not be concerned with all the aspects of software development.

With Polymer.dart (https://www.dartlang.org/polymer-dart/), a new approach towards developing web applications with web components will be discovered, allowing a developer to divide a web page into sections and re-use an already developed and tested web component for each section. In the near future, different catalogs of web components will appear, enabling an engineering approach to software development after waiting for many years. A web component may be composed from other web components. It may pass data to its components. A web component may also inherit its behavior from another web component. It may access an already instantiated web component.

Spiral approach

The spiral approach to software learning and development, which preserves a project history as a series of code snapshots or spirals, is used in this book.

The following three points are important in the spiral approach:

  • The history of development is preserved

  • Simple solutions are provided first; later on, these solutions may be replaced by more advanced solutions

  • Only concepts used in a spiral are explained

All of these three points are important in teaching and learning technologies.

Learning new software concepts and technologies is a challenging task. Learning in spirals, from simple to more advanced concepts but with concrete software applications, helps readers get a reasonable confidence level early on, and motivates them to learn by providing more useful applications. With each new spiral, the project grows and new concepts are introduced. A new spiral is explained with respect to the previous one. The difference between two consecutive spirals is that the next spiral has the new code introduced and the old code modified or deleted. This is named learning by anchoring to what we already understand. With a new spiral, we can come back to what we did previously and improve it. In this way, learning in spirals can touch the same topic several times, but each time with more details in a better version.

What this book covers

Learning Dart has 12 chapters. It begins with basic elements of Dart and it ends with a client/server application that uses MongoDB (http://www.mongodb.org/) for data persistence on the server side.

Chapter 1, Dart – A Modern Web Programming Language, helps you understand what Dart is all about. Dart is presented as a major step forward in the web programming arena.

Chapter 2, Getting to Work with Dart, lets you get a firm grasp on how to program in Dart. The code and data structures in Dart, and its functional principles, are explained by exploring practical examples.

Chapter 3, Structuring Code with Classes and Libraries, lets you understand how to use Dart classes to organize code. Dart libraries are introduced to show how complex software may be packaged.

Chapter 4, Modeling Web Applications with Model Concepts and Dartlero, enables you to design a small model graphically in the Model Concepts tool, which is developed in Dart. A model is then represented in Dart as several classes that inherit some data and operations from classes of the Dartlero model framework, also developed in Dart.

Chapter 5, Handling the DOM in a New Way, helps you to learn how to access HTML elements in Dart. Some elements will even be created in Dart and placed properly in the Document Object Model (DOM) of a web page. Dart will also handle user events, such as a click on a button. Finally, you will be able to create a simple game in Dart.

Chapter 6, Combining HTML Forms with Dart, lets you enter data in a form that will be validated by HTML5 and Dart. Then, the valid data will be saved in the local storage of a browser.

Chapter 7, Building Games with HTML5 and Dart, lets you create, step-by-step, a well-known memory game based on what you have learned already. Each step will be a new spiral represented as a complete project in Dart Editor. The first spiral will draw only a rectangle, while the last spiral will be a game that you may show to your friends.

Chapter 8, Developing Business Applications with Polymer Web Components, helps you to create several web components by using Polymer.dart. Those web components will be used in different sections of a single-page application. Three different projects with web components will be presented in this chapter.

Chapter 9, Modeling More Complex Applications with Dartling, explains how a graphical model can be transformed into a JSON document and then used to generate a complete model in Dart, by using the Dartling domain model framework together with its tools. Dartling follows the Model View Controller (MVC) pattern to separate a model from its views.

Chapter 10, MVC Web and UI Frameworks in Dart – An Overview, introduces you to different frameworks already developed in Dart. Because Dart is a brand new language, those frameworks are at early stages of their useful life.

Chapter 11, Local Data and Client-Server Communication, explains how you can store application data in a local database named IndexedDB, which will then be sent as a JSON document to a Dart server. Asynchronous programming with futures will also be covered in this chapter.

Chapter 12, Data-driven Web Applications with MySQL and MongoDB, explains how you can use database drivers to save (and load) data to (and from) a relational database and a NoSQL database. Data sent from a browser as a JSON document will easily be saved in MongoDB in the same JSON form. Two clients will exchange data with the server so that both of them will be up-to-date.

What you need for this book

In order to benefit from this book, you need to have some basic experience in programming. It is also useful to have some understanding of HTML and CSS. What you really need to bring is your enthusiasm to learn how to become a web developer of the future. All the software used in the book are freely available on the Web:

One of the authors has already taught three times an introductory course to programming with some material from this book. The book also has its own website at http://www.learningdart.org/. Other educational resources for Dart can be found at http://ondart.me/.

Who this book is for

The book is intended for web application programmers, game developers, and other software engineers. Because of its dual focus (Dart and HTML5), the book can appeal to both web developers who want to learn a modern way of developing web applications, and to developers who seek guidance on how to use HTML5. The audience would include mainstream programmers coming with an object-oriented background (Java, .NET, C++, and so on) as well as web programmers using JavaScript, who seek a more structured and tooled way of developing. Both groups would leverage their existing knowledge and expertise: the first, by offering them a way of developing modern web applications using techniques they already know, and the second, by giving them a more productive and engineered way of developing (business) web applications. The article at the following link describes well what Dart has to offer for the web developers of the future:

http://news.cnet.com/8301-1023_3-57613760-93/mixbook-sees-perfect-storm-for-googles-dart-language-q-a/.

Conventions

In this book, you will find a number of styles of text that distinguish among different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The calculateRabbits function calculates and returns an integer value; this is indicated by the word int preceding the function name."

A block of code is set as follows:

void main() {
  print("The number of rabbits increases as:\n");
  for (int years = 0; years <= NO_YEARS; years++) {
    rabbitCount = calculateRabbits(years);             
    print("After $years years:\t $rabbitCount animals");
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

void main() {
  print("The number of rabbits increases as:\n");
  for (int years = 0; years <= NO_YEARS; years++) {
    rabbitCount = calculateRabbits(years);
    print("After $years years:\t $rabbitCount animals");
}

Any command-line input or output is written as follows:

git clone git://github.com/dzenanr/collision_clones.git

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "You can change this behavior by navigating to Tools | Preferences | Run and Debug, and change the Break on Exceptions to None."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.