Lesson 5 of 51 beginner

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

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. 1. The main function where our program starts
  2. 2. Opening curly brace
  3. 3. A comment explaining var
  4. 4. Creating a variable 'city' with value 'Dhaka' - can be changed
  5. 5. Changing city to 'Tokyo' - allowed because we used var
  6. 6.
  7. 7. A comment explaining final
  8. 8. Creating a final variable 'birthYear' set to 2015 - locked after this
  9. 9. A comment showing that reassignment would cause an error
  10. 10.
  11. 11. A comment explaining const
  12. 12. Creating pi as a compile-time constant
  13. 13. Creating maxScore as a compile-time constant
  14. 14.
  15. 15. A comment for data types section
  16. 16. An int variable storing the age 10
  17. 17. A double variable storing the height 4.5
  18. 18. A String variable storing a name
  19. 19. A bool variable storing true
  20. 20.
  21. 21. A comment for type inference
  22. 22. Dart infers this is an int from the value 95
  23. 23. Dart infers this is a double from the value 19.99
  24. 24. Dart infers this is a String from the text
  25. 25. Dart infers this is a bool from true
  26. 26.
  27. 27. A comment previewing string interpolation
  28. 28. Printing name using $ to embed the variable in the string
  29. 29. Printing age and height using $ interpolation
  30. 30. Printing the boolean value
  31. 31. Printing score out of maxScore
  32. 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

Open interactive version (quiz + challenge) ← Back to course: Flutter & Dart