Variables & Data Types
Declare a variable with var and Dart infers its type
from the assigned value.
Example: the three declaration keywords
void main() {
var name = 'Ada'; // type inferred, can be reassigned
final age = 21; // set once, never reassigned
const pi = 3.14159; // a true compile-time constant
print('$name is $age years old.');
}
Ada is 21 years old.
Use final for a value that's set once and never
reassigned after that (common for something computed at runtime, like a
result from an API call). Use const specifically for a
value known and fixed at compile time — a genuine constant, not just "I
don't plan to change this." In practice, prefer final by
default for anything that won't be reassigned, and reach for
var only when a variable genuinely needs to change later.
void main() {
String name = 'Ada';
int age = 21;
print('$name is $age years old.');
}
Ada is 21 years old.