Dart Data Types
Understanding different types of data in Dart
📊 What are Dart Data Types?
Dart data types define what kind of values variables can store. Dart supports numbers, strings, booleans, lists, and more to handle different data efficiently.
// Different data types in Dart
int age = 25;
String name = 'John';
bool isStudent = true;
Common Dart Data Types
Numbers
int and double for numeric values
int count = 10;
double price = 99.99;
Strings
Text data using single or double quotes
String message = 'Hello World';
String name = "Alice";
Booleans
True or false values
bool isActive = true;
bool isComplete = false;
Lists
Collections of items
List fruits = ['apple', 'banana'];
List numbers = [1, 2, 3];
🔹 Number Types
Dart has two main number types:
// Integer numbers
int age = 25;
int temperature = -10;
// Decimal numbers
double height = 5.9;
double pi = 3.14159;
// num can be either int or double
num score = 85;
num average = 87.5;
Output:
age: 25
height: 5.9
score: 85
🔹 String Type
Strings store text data:
// Single quotes
String firstName = 'John';
// Double quotes
String lastName = "Doe";
// Multi-line strings
String address = '''
123 Main Street
New York, NY
''';
// String interpolation
String fullName = '$firstName $lastName';
print(fullName); // John Doe
Output:
John Doe
🔹 Boolean Type
Booleans represent true/false values:
bool isLoggedIn = true;
bool hasPermission = false;
// Using in conditions
if (isLoggedIn) {
print('Welcome back!');
}
// Boolean operations
bool canAccess = isLoggedIn && hasPermission;
Output:
Welcome back!
🔹 Collection Types
Store multiple values together:
🔸 Lists (Arrays)
// List of strings
List colors = ['red', 'green', 'blue'];
// List of numbers
List scores = [85, 92, 78];
// Adding items
colors.add('yellow');
print(colors); // [red, green, blue, yellow]
🔸 Maps (Key-Value pairs)
// Map with string keys
Map ages = {
'Alice': 25,
'Bob': 30,
'Charlie': 35
};
print(ages['Alice']); // 25
Output:
[red, green, blue, yellow]
25