Basics
Hello
Main and Interpolation
Every Dart program runs from main. String interpolation with $name builds output without + concatenation.
Program
Play the program to watch a name flow into a greeting and print.
hello.dart
Replay: real traced execution (multi-file project)
void main() {
var name = 'Ada';
var greeting = 'Hello, $name!';
print(greeting);
}
name ← Ada
1void main() {2 var name = 'Ada';3 var greeting = 'Hello, $name!';values this stepAdanamegreeting ← Hello, Ada!
2var name = 'Ada';3var greeting = 'Hello, $name!';4print(greeting);values this stepHello, Ada!greetingAdanameprint(greeting);
3 var greeting = 'Hello, $name!';4 print(greeting);5}outputHello, Ada!values this stepHello, Ada!greeting
Follow the Greeting
namestarts asAda.- The greeting text uses the current
name. greetingbecomesHello, Ada!.- The program prints
Hello, Ada!. | value | result | | --- | --- | |name| Ada | |greeting| Hello, Ada! | | stdout |Hello, Ada!|
main
`void main()` is the program entry point.
var
`var` infers the variable's type from the initializer.
interpolation
`'$name'` embeds a value inside a string.
Exercise: hello.dart
Reproduce Hello, Ada!, then identify which value is inserted into the greeting.