Variables & Data Types
Storing and Labeling Your Data
Open interactive version (quiz + challenge)Real-world analogy
Variables are like labeled jars in a kitchen. One jar says 'Sugar' and holds sugar. Another says 'Rice' and holds rice. You cannot put rice in the sugar jar! In Dart, each variable has a label (name) and a type (what kind of data it holds). 'var', 'final', and 'const' are different types of jars -- some let you swap the contents, others are sealed shut forever.
What is it?
Variables are named containers that store data. Dart has three ways to declare them: 'var' (changeable), 'final' (set once at runtime), and 'const' (set at compile time). The main data types are int (whole numbers), double (decimals), String (text), bool (true/false), and dynamic (any type).
Real-world relevance
Every app uses variables constantly. A user's name is a String, their age is an int, their account balance is a double, and whether they are logged in is a bool. Choosing the right type prevents bugs and makes your code clearer.
Key points
- var - Flexible Containers — The 'var' keyword creates a variable whose value you can change later. Dart automatically figures out the type from the first value you assign. Once the type is set, you can only store that same type.
- final - Set Once, Done — 'final' creates a variable you can only set ONCE. After the first assignment, it is locked forever. Use final when you know a value will not change but it is calculated at runtime (like current time or user input).
- const - Compile-Time Constants — 'const' is even stricter than final. The value must be known at COMPILE TIME (before the app runs). Use const for values that never change and are always the same, like pi or gravity.
- int - Whole Numbers — 'int' stores whole numbers without decimals: 1, 42, -7, 1000000. Use int for counts, ages, quantities, indices -- anything that is a complete number without a fractional part.
- double - Decimal Numbers — 'double' stores numbers with decimal points: 3.14, 99.99, -0.5. Use double for prices, measurements, percentages -- anything that needs precision beyond whole numbers.
- String - Text Data — 'String' holds text. Wrap text in single quotes ('hello') or double quotes ("hello"). Dart developers usually prefer single quotes. Strings can contain letters, numbers, symbols, spaces -- any text you want.
- bool - True or False — 'bool' stores only two values: true or false. Booleans are used for decisions: Is the user logged in? Is dark mode on? Is the form valid? Every if/else statement uses a boolean to decide what to do.
- dynamic and Object — 'dynamic' turns off type checking -- the variable can hold ANY type and change types. Use it sparingly! 'Object' is the parent of all Dart types. Prefer specific types for safety; use dynamic only when truly needed.
- Type Annotations vs Inference — You can explicitly write the type (String name = 'Alice') or let Dart figure it out (var name = 'Alice'). Both work! Explicit types are clearer for complex code. Inference is great for simple, obvious cases.
Code example
void main() {
// var - can be reassigned
var city = 'Dhaka';
city = 'Tokyo'; // OK!
// final - set once, cannot change
final birthYear = 2015;
// birthYear = 2016; // Error!
// const - compile-time constant
const pi = 3.14159;
const maxScore = 100;
// Data types
int age = 10;
double height = 4.5;
String name = 'Flutter Learner';
bool isHappy = true;
// Type inference - Dart figures it out
var score = 95; // int
var price = 19.99; // double
var greeting = 'Hi!'; // String
var active = true; // bool
// String interpolation (preview!)
print('Name: $name');
print('Age: $age, Height: $height');
print('Is happy? $isHappy');
print('Score: $score / $maxScore');
}Line-by-line walkthrough
- 1. The main function where our program starts
- 2. Opening curly brace
- 3. A comment explaining var
- 4. Creating a variable 'city' with value 'Dhaka' - can be changed
- 5. Changing city to 'Tokyo' - allowed because we used var
- 6.
- 7. A comment explaining final
- 8. Creating a final variable 'birthYear' set to 2015 - locked after this
- 9. A comment showing that reassignment would cause an error
- 10.
- 11. A comment explaining const
- 12. Creating pi as a compile-time constant
- 13. Creating maxScore as a compile-time constant
- 14.
- 15. A comment for data types section
- 16. An int variable storing the age 10
- 17. A double variable storing the height 4.5
- 18. A String variable storing a name
- 19. A bool variable storing true
- 20.
- 21. A comment for type inference
- 22. Dart infers this is an int from the value 95
- 23. Dart infers this is a double from the value 19.99
- 24. Dart infers this is a String from the text
- 25. Dart infers this is a bool from true
- 26.
- 27. A comment previewing string interpolation
- 28. Printing name using $ to embed the variable in the string
- 29. Printing age and height using $ interpolation
- 30. Printing the boolean value
- 31. Printing score out of maxScore
- 32. Closing the main function
Spot the bug
void main() {
const currentTime = DateTime.now();
final pi = 3.14159;
var name = 'Alice';
name = 42;
print('$name $pi $currentTime');
}Need a hint?
Check each variable declaration -- is the keyword appropriate for the value? Can you change a variable's type?
Show answer
Two bugs: (1) const currentTime = DateTime.now() is wrong because DateTime.now() is not known at compile time -- change const to final. (2) name = 42 is wrong because name was inferred as String but 42 is an int -- you cannot change types.
Explain like I'm 5
Variables are like labeled jars on a shelf. The label says what is inside (name, age, score), and the type says what KIND of thing goes in (text, numbers, yes/no). 'var' is a jar with a loose lid -- you can open it and swap what is inside. 'final' is a jar with a glued lid -- once you put something in, it stays forever. 'const' is a jar that was sealed at the factory before it even reached your shelf!
Fun fact
Dart has 'sound null safety' since Dart 2.12, meaning the compiler guarantees that non-nullable variables can NEVER be null at runtime. This eliminates an entire category of crashes that plague other languages. Tony Hoare, who invented null, called it his 'billion-dollar mistake'!
Hands-on challenge
Open DartPad and declare variables of every type: an int for your age, a double for your height, a String for your name, and a bool for whether you like coding. Print them all using print(). Try changing a final variable and see the error!
More resources
- Dart Variables (Dart Official)
- Dart Built-in Types (Dart Official)
- Dart Type System (Dart Official)