CHAPTER 2 ■ YOUR FIRST DART PROGRAMS
13
element with the id of “name.” The left side of an assignment operator is always what we are assigning to, and the
right side is what we are assigning from. (querySelector("#name_box") as InputElement) is a way of saying “find
the HTML element with the id of ‘name_box’ and represent it as an InputElement.” An InputElement is a special type
of HTML element that receives input from the user. In this case, as defined in our HTML document, our “name_box”
receives text input from the user. .value accesses the value of the user’s input. This is ultimately what is assigned back
to the text of our “name” element. In short, this line says “assign the value of the HTML input element with the id of
‘name_box’ to the text property of the HTML element with the id of ‘name’.”
(querySelector("#name_box") as InputElement).value = "";
By reading over again the description of the previous line, you should be able to understand this one. In short,
“Find the HTML element with the id of ‘name_box’. Represent it as an InputElement. Set its value to be blank.” Earlier,
we learned that double quotes are used to define a string literal. Two double quotes next to each other with nothing
between them represent an empty string, a blank if you will. The goal here is to reset the text box to be blank when the
user clicks the button.
}
The last closing bracket is where the function sayHello() ends. It is also the end of our program.
For only three lines of real “meat,” our program may seem pretty complicated. This is both a testament to how
compact yet expressive the Dart programming language can be and how early we are on our journey of learning Dart.
You should now try running the program. You will notice that it automatically loads in Dartium, the Chrome-
based web browser with a built-in virtual machine for the Dart language. Play around with the result. Try entering a
name, clicking the button, erasing the name, and then clicking the button again. Why does it erase the name that was
previously displayed?
Input and Output
While seemingly obvious concepts, understanding input and output (sometimes abbreviated to I/O or IO) is
fundamental to understanding how to write a good program. In short, input is information that the computer takes in
for processing, and output is information that the computer delivers to the user.
An input device is a tool that helps the user deliver input to the computer. The input devices we’ll be using in this
book will primarily be the keyboard and the mouse. Other input devices you may be familiar with include a touch
screen, Wacom tablet, microphone, and camera. An output device does the opposite. The main output device we will
be using is the monitor (or “screen” to be more generic), but another you are surely familiar with is the printer.
When we write command-line applications with Dart, only the keyboard is used as an input device and our only
output is textual in nature. Our first HelloWorld program was such a command-line program and didn’t even take
keyboard input—it took no input at all. As we saw, print() is used in a command-line application to output text to the
user. Hello World Fancy was a web-based program with a graphical user interface
7
(GUI). It took input via a text box
and a button (yes, even a click of the mouse is a type of input) and delivered textual output on a web page. Did you
ever think of your headphones as an output device? You should, because they are one.
As our programs get more advanced, it will be important to think about the best ways to take input from the user
and the best ways to present output to the user. The makers of operating systems (such as Apple and Microsoft) have
guidelines about how to design user interfaces for developers of programs that run on their platforms. On the Web, it
is more of a Wild West type of atmosphere. This accounts for why some web companies spend as much on designers
as they do on developers. The entire area of input/output with regard to user interface design falls under an academic
discipline known as Human-Computer Interaction.
7
As opposed to a command-line interface, a graphical user interface utilizes graphical elements, such as pictures, text boxes,
buttons, and menus, for human-computer interaction. It is the main interface for interacting with computers running OS X
and Windows.
www.it-ebooks.info