You are previewing Learning Dart - Second Edition.
O'Reilly logo
Learning Dart - Second Edition

Book Description

Learn to develop high performance applications with Dart 1.10

About This Book

  • Develop apps for the modern web using Dart and HTML5

  • Clarify and shorten your Dart code using enums

  • Build a complex UI for business applications with Dart's Polymer framework, based on web components

  • Who This Book Is For

    If you want to become a developer for the modern web, or wish to add Dart to your tool belt, then this book is for you. The book assumes you have basic HTML experience and know how web applications work. Some previous programming experience, preferably in a modern language like C#, Java, Python, Ruby or JavaScript, will give you a head start. You can work with Dart on your preferred platform, be it Linux, Mac OS X or Windows.

    What You Will Learn

  • Structure your code using functions, classes, generics, packages and libraries

  • Use the power of modern browsers to process and store data

  • Make games by drawing, and using audio and video in the browser

  • Develop an application with a model-driven and spiral-paced approach

  • Discover the Observatory tools for profiling memory and CPU usage of Dart programs

  • Store your app's data in MySQL and MongoDB through Dart

  • Build powerful HTML5 forms, validate and store data in local storage, and use web components to build your own user interface

  • Run your Dart server on an App Engine Managed VM

  • In Detail

    Dart is an open source programming language for the web, developed at Google, with a steadily growing community. It is a single language for both client and server, appropriate for the full range of devices on the web – including phones, tablets, laptops, and servers. It encompasses the lessons of the last two decades of web programming.

    This book will give you a thorough overview of Dart, taking you through its ecosystem, syntax, and development principles.

    With this book, you will build web games using HTML5, audio, and video, and also dive into processing and displaying data in HTML5 forms with Dart. You will also learn how web components fit together with HTML5, and how to apply them in business web applications of the future. You will discover how to store data on the client, communicate data between client and server with JSON, and store JSON data with MongoDB and MySQL.

    Stop solving new challenges with the same old tools – let Dart show you a whole new way.

    Style and approach

    This book provides you a project-based approach, with everything you need to start or enhance your career in the future of web development with Dart. It follows the spiral approach: each project builds up in successive spirals, adding new features in each step.

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

    Table of Contents

    1. Learning Dart Second Edition
      1. Table of Contents
      2. Learning Dart Second Edition
      3. Credits
      4. About the Authors
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. The spiral approach
        2. What this book covers
        3. What you need for this book
        4. Who this book is for
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Dart – A Modern Web Programming Language
        1. What is Dart?
        2. Dart is a perfect fit for HTML5
          1. A very short history of web programming
          2. Dart empowers the web client
          3. JavaScript is not the way to go
          4. Google, GWT, and Dart
          5. Advantages of Dart
        3. Getting started with Dart
          1. Installing the Dart Editor
          2. Other options for working with Dart code
            1. Your first Dart program
        4. Getting a view on the Dart tool chain
          1. The Dart execution model
        5. A bird's eye view on Dart
          1. Example 1 – raising rabbits
            1. Extracting a function
            2. A web version
          2. Example 2: banking
        6. Making a to-do list with Dart
        7. Summary
      9. 2. Getting to Work with Dart
        1. Variables – to type or not to type
        2. Built-in types and their methods
          1. Conversions
          2. Operators
          3. Some useful String methods
          4. Dates and times
          5. List
          6. Maps
        3. Documenting your programs
        4. Changing the execution flow of a program
        5. Using functions in Dart
          1. Return types
          2. Parameters
          3. First class functions
        6. Using enums
        7. Recognizing and catching errors and exceptions
          1. A debugging exercise
        8. Summary
      10. 3. Structuring Code with Classes and Libraries
        1. A touch of class – how to use classes and objects
          1. Visibility – getters and setters
          2. Types of constructors
            1. Named constructors
            2. Factory constructors
            3. The const constructors
            4. Inheritance
            5. Abstract classes and methods
            6. The interface of a class – implementing interfaces
            7. Polymorphism and the dynamic nature of Dart
          3. Collection types and generics
            1. Typing collections and generics
            2. The collection hierarchy and its functional nature
          4. Structuring your code using libraries
            1. Using a library in an app
            2. Resolving name conflicts
            3. Visibility of objects outside a library
          5. Managing library dependencies with pub
          6. Unit testing in Dart
          7. Asynchronous programming with async and await
          8. Project – word frequency
          9. The Observatory tool
        2. Summary
      11. 4. Modeling Web Applications with Model Concepts and Dartlero
        1. A short introduction to Git and GitHub
          1. Creating a repository on GitHub and a local version
          2. Collaborating on a GitHub project
        2. What is a model and why we need it in programming
        3. Model Concepts – a graphical design tool for our models
          1. Working with model concepts
            1. Explaining the model
            2. Drawing the model
            3. Exporting the model
            4. What is JSON?
        4. Dartlero – a simple domain model framework
        5. An example of using Dartlero
        6. The categories and links application
        7. Summary
      12. 5. Handling DOM in a New Way
        1. Finding elements and changing their attributes
          1. Finding elements
          2. Changing the attributes of elements
        2. Creating and removing elements
        3. Handling events
        4. Manipulating the style of page elements
        5. Animating a game
          1. Ping Pong using style(s)
          2. How to draw on a canvas – Ping Pong revisited
            1. Spiral 1 – drawing a circle and a rectangle
            2. Spiral 2 – colored circles and rectangles
            3. Spiral 3 – moving a ball
            4. Spiral 4 – reorganizing the code
            5. Spiral 5 – a bouncing ball
            6. Spiral 6 – displaying the racket
            7. Spiral 7 – moving the racket using keys
            8. Spiral 8 – moving the racket using the mouse
            9. Spiral 9 – a real game
            10. Spiral 10 – title and replay
            11. Spiral 11 and the master version
        6. Summary
      13. 6. Combining HTML5 Forms with Dart
        1. Spiral 1 – the power of HTML5 forms
        2. Spiral 2 – how to validate data with Dart
          1. Validation in the model
        3. Spiral 3 – how to store data in a local storage
        4. Spiral 4 – reading and showing data
        5. Spiral 5 – changing and updating data
        6. Spiral 6 – working with a list of bank accounts
        7. Summary
      14. 7. Building Games with HTML5 and Dart
        1. The model for the memory game
        2. Spiral 1 – drawing the board
        3. Spiral 2 – drawing cells
        4. Spiral 3 – coloring the cells
        5. Spiral 4 – implementing the rules
        6. Spiral 5 – game logic (bringing in the time element)
        7. Spiral 6 – some finishing touches
        8. Spiral 7 – using images
        9. Adding audio to a web page
        10. The Collision Clones game
        11. Adding video to a web page
        12. Summary
      15. 8. Developing Business Applications with Polymer Web Components
        1. How web components change web development
        2. Web components with Polymer.dart
          1. Declaring and instantiating a web component
        3. Two-way data binding in Polymer.dart
        4. Creating the web_links project
          1. Spiral s01
          2. Spiral s02
          3. Spiral s04
          4. Spiral s05
          5. Spiral s06
        5. Using Polymer for the category links project
          1. Adding local storage
        6. Applying web components to the project tasks app
          1. The Add and Remove task propagations
        7. Summary
      16. 9. Modeling More Complex Applications with dartling
        1. The dartling domain modeling framework
        2. Design of the Travel Impressions model in spirals
        3. Generating the Travel Impressions code from the model
        4. Initializing the Travel Impressions model with data
        5. Testing the Travel Impressions model
        6. Defining and using the MVC pattern
        7. The TodoMVC app
          1. Spiral 0 – generating a class model
          2. Spiral 1 – adding todo tasks
          3. Spiral 2 – showing how many todo tasks are left
          4. Spiral 3 – removing a single task and completed tasks
          5. Spiral 4 – saving in the local storage
          6. Spiral 5 – displaying completed todos
          7. Spiral 6 – editing a task and testing the model
        8. Summary
      17. 10. Local Data and Client-Server Communication
        1. The options for browser storage
        2. Asynchronous calls and Future objects
        3. Using IndexedDB with Dart
          1. Spiral s00
          2. Spiral s01
          3. Spiral s02
          4. Spiral s03
          5. Spiral s04
          6. Spiral s05
        4. Using Lawndart
        5. A Dart web server
        6. Using JSON web services
          1. Spiral s07
        7. Summary
      18. 11. Data-Driven Web Applications with MySQL and MongoDB
        1. Database drivers for Dart
        2. Storing todo data in MySQL
        3. Dartlero tasks – a many-to-many model in MySQL
          1. The JSON storage
          2. MySQL storage
        4. MongoDB – a NoSQL database
        5. Using the mongo_dart driver to store the todo data in MongoDB
        6. Running a Dart server on an App Engine Managed VM
        7. Summary
      19. Index