Tutorial: Dartboard & Language Basics
Follow this tutorial to modify and run small Dart programs inside your browser window, using the Dartboard app.
main() {
var name = 'World';
print('Hello, ${name}!');
}
About the code
This example illustrates some basic Dart features and conventions:
- Top-level
main()function -
Having a
main()function with no surrounding class lets Dart know where to start executing your code. - Variable declared with
var -
Whenever you create a variable in a Dart program,
you must use
the
varkeyword, thefinalkeyword, or a type name to declare that variable. - Output with
print() -
Dart's
print()function sends text to the console. - String literals
-
You can use either single or double quotes to mark strings:
'World'and"World"are equally valid. - String interpolation with
${expression} -
Dart lets you embed expressions within string literals.
If the expression is just a variable,
as it is in this example,
then you can omit the curly braces (
{}). Another alternative for building strings is the plus operator (+). For example, the following three statements are equivalent:print('Hello, ${name}!'); print('Hello, $name!'); print('Hello, ' + name + '!');
Try this
- Edit the code and click the Run button (the button at the upper left of Dartboard) to compile and run the new code.
-
Some changes to try:
- Change
'World'to some other string—maybe'Mondo'or"you". -
Change
${name}to$name. -
Embed an arithmetic expression in the output string.
For example:
var age = 21; print("Happy birthday, $name, you're ${++age} today.");
- Change
Tips for using Dartboard
- Dartboard indicates warnings with yellow flags and errors with red flags. Mouse over the flag to see a description of the problem.
- When you edit the code originally displayed by Dartboard, a Reload button appears at the top of the Dartboard. Click it to go back to the original code.
- The usual keyboard shortcuts should work in Dartboard. For example, in Google Chrome on a Mac , Command-Z undoes your last change, and Shift-Command-Z re-applies it.
The next page introduces classes in the Dart language.
