Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Dart

You're reading from   Learning Dart Dart is the programming language developed by Google that offers a new level of simple versatility. Learn all the essentials of Dart web development in this brilliant tutorial that takes you from beginner to pro.

Arrow left icon
Product type Paperback
Published in Jan 2014
Publisher
ISBN-13 9781849697422
Length 388 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (14) Chapters Close

Preface 1. Dart – A Modern Web Programming Language 2. Getting to Work with Dart FREE CHAPTER 3. Structuring Code with Classes and Libraries 4. Modeling Web Applications with Model Concepts and Dartlero 5. Handling the DOM in a New Way 6. Combining HTML5 Forms with Dart 7. Building Games with HTML5 and Dart 8. Developing Business Applications with Polymer Web Components 9. Modeling More Complex Applications with Dartling 10. MVC Web and UI Frameworks in Dart – An Overview 11. Local Data and Client-Server Communication 12. Data-driven Web Applications with MySQL and MongoDB Index

Making a todo list with Dart

Since this has become the "Hello World" for web programmers, let's make a simple todo list and start a new web application todo_v1. To record our tasks we need an input field corresponding with InputElement in Dart:

<input id="task" type="text" placeholder="What do you want to do?"/>

The HTML5 placeholder attribute lets you specify default text that appears in the field.

We specify a list tag (UListElement) that we will fill up in our code:

<ul id="list"/>

The following is the code from todo_v1.dart:

import 'dart:html';

InputElement task;
UListElement list;

main() {
  task = querySelector('#task');                        (1)
  list = querySelector('#list');                        (2)
  task.onChange.listen( (e) => addItem() );             (3)
}

void addItem() {
  var newTask = new LIElement();                       (4)
  newTask.text = task.value;                           (5)
  task.value = '';                                     (6)
  list.children.add(newTask);                          (7)
}

We bind our HTML elements to the Dart objects task and list in lines (1) and (2). In line (3) we attach an event-handler addItem to the onChange event of the textfield task: this fires when the user enters something in the field and then leaves it (either by pressing Tab or Enter). UListElement is in fact a collection of LIElements (these are its children); so for each new task we make a LIElement (4), assign the task's value to it (5), clear the input field (6), and add the new LIElement to the list in (7). In the following screenshot you can see some tasks to be performed:

Making a todo list with Dart

A simple todo list

Of course this version isn't very useful (unless you want to make a print of your screen); our tasks aren't recorded and we can't indicate which tasks are finished. Don't worry; we will enhance this app in the future versions.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image