Dart, lesson 2
A variable is a name that holds a value so you can use it later. In Dart you usually
declare one with var and let Dart work out the type from the value you give it:
Once Dart has decided that visitors is an int, it stays an int. Trying to put a string
into it later is a compile error, which catches many mistakes before the program even runs.
| Type | Holds | Example |
|---|---|---|
int | whole numbers (64-bit) | 42, -7 |
2 exercises to try what you just read.
double |
| numbers with a fractional part |
3.14, -0.5 |
String | text | 'hello' |
bool | true or false | true |
You can also write the type yourself instead of var. This is handy when you want to be
explicit, or when the variable has no starting value yet:
Dart has no "truthy" values. An if needs a real bool, so you write count == 0
rather than relying on count itself.
By default, a variable can never be null. If a value might be missing, add ? to the
type. Dart then makes you deal with the missing case before you use it:
You will meet this again when reading input, because a line of input might not exist.
If a value should never change after you set it, say so:
final can be assigned once, and the value can be computed while the program runs.const must be known when the program is compiled, like a literal number or string.Prefer final for anything you don't plan to reassign. It tells readers (and the
compiler) that the value is settled.
Variable names use lowerCamelCase: start with a lowercase letter and capitalize each new
word (totalPrice, maxSpeed). Names may contain letters, digits, _ and $, but cannot
start with a digit. Pick names that say what the value means, not what type it is.
Every value has toString(). Numbers convert the other way with int.parse and
double.parse, and a double can be rounded for display with toStringAsFixed:
Create these variables with sensible types: a const name 'Nova', a final level 12,
and a var health 87.5. Then print them in exactly this format:
The program reads no input.
A shop stores prices as text. Start from the string '19.99' and a quantity of 3.
Convert the price with double.parse, multiply by the quantity, and print the total with
exactly two decimals. Then print the quantity converted to text with toString() followed
by items.
Expected output:
void main() {
var city = 'Lisbon'; // a String
var visitors = 1200; // an int
var rating = 4.5; // a double
var isOpen = true; // a bool
print('$city: $visitors visitors, rated $rating, open: $isOpen');
}int score = 0;
String player = 'Ada';
double price = 9; // an int literal becomes 9.0 hereString? nickname; // starts as null
nickname = 'Ace';
print(nickname.length); // OK: Dart can see it was just setconst daysInWeek = 7; // fixed forever
final greeting = 'Hi, ' + 'Sam'; // set once, never reassigned
var lives = 3; // can change
lives = lives - 1;var n = int.parse('42'); // 42
var text = 3.14159.toStringAsFixed(2); // '3.14'
var whole = 7.9.toInt(); // 7 (drops the fraction)Player: Nova
Level: 12
Health: 87.5Total: 59.97
3 items