Basics
Variables
var, final, const
var allows reassignment, final is assigned once at runtime, and const is a compile-time constant.
Program
Play the program to change count, then capture a final total.
variables.dart
Replay: real traced execution (multi-file project)
void main() {
var count = 1;
count = count + 2;
final total = count * 10;
const tax = 5;
print(total + tax);
}
count ← 1
1void main() {2 var count = 1;3 count = count + 2;values this step1countcount ← 3
2var count = 1;3count = count + 2;4final total = count * 10;values this step1 → 3counttotal ← 30
3count = count + 2;4final total = count * 10;5const tax = 5;values this step30total3counttax ← 5
4final total = count * 10;5const tax = 5;6print(total + tax);values this step5taxprint(total + tax);
5 const tax = 5;6 print(total + tax);7}output35values this step30total5tax
Follow the Values
countstarts at1.count + 2changescountto3.- The final
totalis30. taxis the constant5.- The program prints
35. | name | value | | --- | --- | | startingcount| 1 | | updatedcount| 3 | |total| 30 | |tax| 5 | | stdout |35|
var
Reassignable runtime binding.
final
Assigned once; the value can come from any expression.
const
Compile-time constant; value must be known at compile time.
Exercise: variables.dart
Reproduce the output 35, then identify the updated count, total, and tax values that lead to it.