Dart Data Types Explained

1. int (Integer)

int is used to store whole numbers (positive or negative) without decimals.

int age = 25;
print(age);

2. double

double is used to store numbers with decimal points.

double pi = 3.14;
print(pi);

3. String

String is used to store text or a sequence of characters enclosed in quotes.

String name = "Sumit";
print(name);

4. bool (Boolean)

bool is used to store true or false values (yes or no).

bool isFlutterFun = true;
print(isFlutterFun);

5. List

List is used to store multiple values in a single variable (like an array).

List fruits = ["Apple", "Banana", "Orange"];
print(fruits);

6. Map

Map is a collection of key-value pairs.

Map user = {
  "name": "Rohan",
  "city": "Delhi"
};
print(user);

7. var and dynamic

See about const and final

var is used when you want Dart to automatically detect the type.
dynamic allows changing the type of value at runtime.

var city = "Mumbai"; // inferred as String
city = "Pune";

dynamic item = "Book";
item = 100; // no error, type changed to int
print(item);

8. Typed vs Untyped List

In Dart, you can define a list with or without a specific type:

🔸 Untyped List

This list allows storing any data type. Not safe for large projects.

List fruits = ["Apple", "Banana", "Orange"];
fruits.add(123); // No error, but not type-safe
print(fruits);

🔹 Typed List

This list is strictly typed. Only specific types (like String) are allowed.

List<String> fruits = ["Apple", "Banana", "Orange"];
fruits.add("Mango"); // OK
fruits.add(123); // ❌ Error: int is not a String
print(fruits);

📝 Why Typed Lists?

📊 Comparison Table

Code Type Type-Safe? Accepts Other Types?
List fruits = [...]; Untyped List No Yes ✅
List<String> fruits = [...]; Typed List (String) Yes ✅ No ❌

Tip: Always use List<Type> for better safety and cleaner code.