- Learning Dart
- Ivo Balbaert Dzenan Ridjanovic
- 274字
- 2021-07-19 18:33:41
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:

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.
- Mastering Machine Learning for Penetration Testing
- Building Django 2.0 Web Applications
- 電子政務效益的經濟分析與評價
- 物聯網檢驗檢測技術
- JBoss EAP6 High Availability
- 新一代物聯網架構技術:分層算力網絡
- 互聯網基礎資源技術與應用發展態勢(2021—2023)
- Drush User’s Guide
- 物聯網安全技術
- Building RESTful Web services with Go
- 圖解手機元器件維修技巧
- VMware NSX網絡虛擬化入門
- 大型企業微服務架構實踐與運營
- 商業的本質和互聯網
- 物聯網與智慧廣電