Dart Variables and Data Types

Learn the basics clearly: Programming definitions + Real-world examples!

1. What is a Variable? (Programming Definition)

A variable in programming is a named container that stores information which can be changed later. Think of it as a label on a box that tells you what's inside, so the program can use or update that information.

2. What is a Variable? (Real-World Example)

Imagine a locker where you keep your school books. The locker has a name or number, so you know where your books are. You can put a new book in the locker or take one out anytime. The locker is like a variable; it holds something, and you can change it.

3. What are Data Types? (Programming Definition)

Data types tell the computer what kind of data a variable can hold, such as numbers, text, or true/false values. This helps the computer understand how to store and use the data correctly.

4. What are Data Types? (Real-World Example)

Think about different kinds of containers: a basket for apples, a box for toys, or a jar for coins. Each container is made for a specific kind of thing. Similarly, data types tell the program what type of "container" to use for your data.

5. Common Dart Data Types

Data Type Programming Explanation Real-World Example Example Code
int Stores whole numbers (no decimals). Like counting how many apples you have: 3 apples. int apples = 3;
double Stores decimal numbers (numbers with fractions). Measuring water: 1.5 liters. double water = 1.5;
String Stores text or words. Your name or a street address. String city = "New York";
bool Stores only true or false values. Whether a light is ON (true) or OFF (false). bool isLightOn = true;
var A variable that automatically detects the type based on the assigned value. A magic box that changes what it holds depending on what you put in. var name = "Alice";
dynamic Variable whose type can change at any time during the program. A container that can first hold toys, then coins later. dynamic item = "toy"; item = 5;
View Datatypes in Detail

6. Simple Example Code

This Dart program uses variables with different data types and prints their values:

void main() {
  int age = 12;
  double height = 4.5;
  String city = "Chicago";
  bool isStudent = true;
  var favoriteColor = "Blue";

  print("Age: $age");
  print("Height: $height feet");
  print("City: $city");
  print("Is student: $isStudent");
  print("Favorite color: $favoriteColor");
}