int (Integer)int is used to store whole numbers (positive or negative) without decimals.
int age = 25;
print(age);
doubledouble is used to store numbers with decimal points.
double pi = 3.14;
print(pi);
StringString is used to store text or a sequence of characters enclosed in quotes.
String name = "Sumit";
print(name);
bool (Boolean)bool is used to store true or false values (yes or no).
bool isFlutterFun = true;
print(isFlutterFun);
ListList is used to store multiple values in a single variable (like an array).
List fruits = ["Apple", "Banana", "Orange"];
print(fruits);
MapMap is a collection of key-value pairs.
Map user = {
"name": "Rohan",
"city": "Delhi"
};
print(user);
var and dynamicvar 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);
In Dart, you can define a list with or without a specific type:
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);
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);
| 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.