Book Image

Learning Dart, Second Edition - Second Edition

By : Ivo Balbaert
Book Image

Learning Dart, Second Edition - Second Edition

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Learning Dart Second Edition
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- or 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 different programming languages as well that a developer must learn, each one more suitable either for the server-side or for the client-side.

Learning Dart will make a developer become more productive by using Dart both for clients and servers. Using the same language, 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 the cases, the performance benchmarks show promising scores (https://www.dartlang.org/performance/). Dart is both an object-oriented and a functional language. A mix of both the approaches is possible with Dart, providing great professional freedom and programming background flexibility. In addition, Dart provides many libraries and tools (http://pub.dartlang.org/) to allow a developer focus on the tasks 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 of developing web applications with web components will allow a developer to divide a web page in sections and reuse an already developed and tested web component for each section. In the near future, different catalogs of web components will appear, enabling, after waiting for many years, an engineering approach to software development. A web component may be derived from other web components. It may pass data to its components. A web component may inherit its behavior from another web component. It may access an already instantiated web component.

The 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 the concepts used in a spiral are explained.

All three points are important to teach and learn these 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 the two consecutive spirals is that the next spiral introduces the new code and modifies or deletes the old. This is called learning by anchoring to what we already understand. With a new spiral, we can go 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, Second Edition, has 11 chapters. It begins with the basic elements of Dart and 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, will help 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, will help 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, will make you understand how to use Dart classes to organize code. Dart libraries will be introduced to show how complex software may be packaged.

Chapter 4, Modeling Web Applications with Model Concepts and Dartlero, will let you design graphically a small model 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 the classes of the Dartlero model framework; this is also done in Dart.

Chapter 5, Handling DOM in a New Way, covers how to access HTML elements in Dart. Some elements will be even 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 HTML5 Forms with Dart, will let you enter some data in a form. The data 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, will let you create a well-known memory game step by step, 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, will help you create several web components using Polymer.dart. These web components will be used in the 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, will let you discover 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. The dartling follows the Model View Controller (MVC) pattern to separate a model from its views.

Chapter 10, Local Data and Client-Server Communication, will let you store application data in a local database called IndexedDB. The data will then be sent as a JSON document to a Dart server. Asynchronous programming with futures will be covered in this chapter.

Chapter 11, Data-Driven Web Applications with MySQL and MongoDB, will help you learn how to 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 be easily 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.

For this second edition, all the code and projects were updated to Dart version 1.11. Moreover, sections on the following topics were added:

  • enums

  • Async and await

  • The Observatory tool

  • Running a Dart server on an App Engine Managed VM

What you need for this book

In order to profit 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 is 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. 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 web developers who want to learn a modern way of developing web applications and to developers who seek how to use HTML5. The audience includes mainstream programmers with an object-oriented background (Java, .NET, C++) as well as web programmers who use JavaScript to seek a more structured and tooled way of developing. Both groups will leverage their existing knowledge and expertise: the book will offer the first way of developing modern web applications using techniques they already know; it will give the second a more productive and engineered way of developing (business) web applications. The following article describes well what Dart has to offer for 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 distinguishe between 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 last clause in the try statement should be on Exception catch(e) or, even better, a simple catch(e) to stop any type of error or exception."

A block of code is set as follows:

import 'dart:math';                                      (1)

void main() {
  var n = 0; // number of rabbits                        (2)

  print("The number of rabbits increases as:\n");        (3)
  for (int years = 0; years <= 10; years++) {            (4)
    n = (2 * pow(E, log(15) * years)).round().toInt();   (5)
    print("After $years years:\t $n animals");           (6)
  }
}

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

try {
  int inp = int.parse(input);
} on FormatException {
  print ('ERROR: You must input an integer!');
}

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

Observatory listening on http://127.0.0.1:49621

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: "Navigate to Tools | Preferences | Run and Debug, and change 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.