Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning Dart
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning Dart

Learning Dart

4.4 (10)
close
close
Learning Dart

Learning Dart

4.4 (10)

Overview of this book

Mastering Dart by Projects is a step-by-step guide that aims to give you hands-on knowledge about programming in Dart using an example-based approach.If you want to become a web developer, or perhaps you already are a web developer but you want to add Dart to your tool belt, then this book is for you. This book assumes that you have at least some knowledge of HTML and how web applications work. Some previous programming experience, preferably in a modern language like C#, Java, Python, Ruby, or JavaScript, will also give you a head start. You can also work with Dart on your preferred platform, be it Linux, Mac OS X, or Windows.
Table of Contents (14 chapters)
close
close
13
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.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY