Dart Data Types
last modified June 4, 2025
Dart is a statically typed language with a rich set of built-in data types. These types help organize and manipulate data in programs efficiently.
The fundamental Dart data types include numbers, strings, booleans, lists, maps, and more. Each type has specific characteristics and operations defined for it.
Numbers: int and double
Dart provides two types for numbers: int for integers and double for floating-point numbers. Both types are subclasses of num.
void main() {
int age = 30;
double price = 19.99;
num temperature = 23.5; // Can hold both int and double
print('Age: $age');
print('Price: $price');
print('Temperature: $temperature');
// Number operations
print('Sum: ${age + price}');
print('Difference: ${price - age}');
}
This example shows basic number type usage. We declare variables of different numeric types and perform arithmetic operations. The num type is flexible.
$ dart main.dart Age: 30 Price: 19.99 Temperature: 23.5 Sum: 49.99 Difference: -10.01
Strings
Strings represent sequences of characters in Dart. They are immutable and can be created using single or double quotes. String interpolation is supported.
void main() {
String greeting = 'Hello';
String name = "Alice";
String message = '$greeting, $name! How are you?';
print(message);
print('Length: ${message.length}');
print('Uppercase: ${message.toUpperCase()}');
print('Contains "Alice": ${message.contains('Alice')}');
// Multi-line string
String multiLine = '''
This is a
multi-line
string''';
print(multiLine);
}
We demonstrate string creation, interpolation, and common operations. Triple quotes create multi-line strings. String methods provide useful functionality.
$ dart main.dart Hello, Alice! How are you? Length: 23 Uppercase: HELLO, ALICE! HOW ARE YOU? Contains "Alice": true This is a multi-line string
Booleans
The bool type represents boolean values true and false. It's used in conditional expressions and logical operations. Dart requires explicit boolean values.
void main() {
bool isRaining = true;
bool isSunny = false;
int temperature = 25;
print('Is raining: $isRaining');
print('Is sunny: $isSunny');
// Logical operations
bool niceWeather = !isRaining && temperature > 20;
print('Nice weather: $niceWeather');
// Conditional expression
String weatherMessage = isRaining ? 'Bring umbrella' : 'Enjoy the day';
print(weatherMessage);
}
This shows boolean variable declaration and usage in logical operations. The ternary operator provides concise conditional expressions based on booleans.
$ dart main.dart Is raining: true Is sunny: false Nice weather: false Bring umbrella
Lists
Lists are ordered collections of objects in Dart. They are similar to arrays in other languages. Lists can be fixed-length or growable.
void main() {
// Growable list
List<String> fruits = ['apple', 'banana', 'orange'];
fruits.add('mango');
// Fixed-length list
List<int> fixedList = List.filled(3, 0);
fixedList[1] = 42;
print('Fruits: $fruits');
print('Fixed list: $fixedList');
// List operations
print('First fruit: ${fruits.first}');
print('Last fruit: ${fruits.last}');
print('Sublist: ${fruits.sublist(1, 3)}');
// List mapping
var lengths = fruits.map((fruit) => fruit.length);
print('Fruit lengths: $lengths');
}
We create both growable and fixed-length lists. Various list operations are demonstrated including mapping elements to new values.
$ dart main.dart Fruits: [apple, banana, orange, mango] Fixed list: [0, 42, 0] First fruit: apple Last fruit: mango Sublist: [banana, orange] Fruit lengths: (5, 6, 6, 5)
Maps
Maps are collections of key-value pairs in Dart. Each key must be unique, and keys and values can be of any type. Maps are unordered by default.
void main() {
// Map literal
Map<String, int> ages = {
'Alice': 30,
'Bob': 25,
'Charlie': 35
};
// Adding entries
ages['David'] = 28;
print('Ages: $ages');
print('Alice\'s age: ${ages['Alice']}');
// Map operations
print('Keys: ${ages.keys}');
print('Values: ${ages.values}');
print('Contains key "Bob": ${ages.containsKey('Bob')}');
// Iterating
ages.forEach((name, age) => print('$name is $age years old'));
}
This example demonstrates map creation, modification, and common operations. We show how to iterate through map entries using forEach.
$ dart main.dart
Ages: {Alice: 30, Bob: 25, Charlie: 35, David: 28}
Alice's age: 30
Keys: (Alice, Bob, Charlie, David)
Values: (30, 25, 35, 28)
Contains key "Bob": true
Alice is 30 years old
Bob is 25 years old
Charlie is 35 years old
David is 28 years old
Best Practices
- Type Safety: Always specify types for better code clarity.
- Constants: Use final/const for values that don't change.
- Null Safety: Use nullable types (?) when values can be null.
- Collections: Prefer literal syntax for list/map creation.
- Type Inference: Use var when type is obvious from context.
Source
Dart Built-in Types Documentation
This tutorial covered Dart's fundamental data types with practical examples demonstrating their usage and common operations.
Author
List all Dart tutorials.