Dart Data Types
Every value in Dart is an object, and every object has a type. Even numbers and booleans are objects with methods you can call — 42.isEven is perfectly valid Dart.
The built-in types
| Type | Example values | Notes |
|---|---|---|
int |
0, -7, 1000000 |
Whole numbers (64-bit on native platforms) |
double |
3.14, -0.5, 1e3 |
64-bit floating-point numbers |
num |
either of the above | The common supertype of int and double |
String |
'hi', "Dart" |
A sequence of UTF-16 code units |
bool |
true, false |
Only these two values |
List<T> |
[1, 2, 3] |
Ordered collection |
Set<T> |
{'a', 'b'} |
Unordered, unique items |
Map<K, V> |
{'id': 1} |
Key–value pairs |
Record |
('Ada', 36) |
Fixed group of values (Dart 3) |
Null |
null |
The type of the value null |
Collections get their own lesson later; here we focus on numbers, strings, booleans and records.
Numbers
void main() {
int apples = 7;
double price = 0.45;
num total = apples * price;
print(total);
print(total.toStringAsFixed(2));
print(17 ~/ 5); // integer division
print(17 % 5); // remainder
print(10 / 4); // / always returns a double
print(42.isEven);
print((-8).abs());
}3.15
3.15
3
2
2.5
true
8
Note:
7 * 0.45is stored in binary floating point, so some results print with tiny rounding errors (for example0.1 + 0.2prints0.30000000000000004). UsetoStringAsFixedwhen displaying money or measurements.
Converting between types
Dart never converts types silently. You convert explicitly with methods such as parse, toString, toInt and toDouble.
void main() {
int fromText = int.parse('123');
double fromText2 = double.parse('9.75');
String asText = 3.5.toString();
print(fromText + 1);
print(fromText2 * 2);
print(asText + '!');
print(9.99.toInt()); // truncates toward zero
print(9.5.round());
print(4.toDouble());
print(int.tryParse('abc')); // null instead of an exception
}124
19.5
3.5!
9
10
4.0
null
Booleans
The bool type has exactly two values. Unlike JavaScript, Dart has no "truthy" values — an if condition must be a real bool, so if (1) or if (name) will not compile.
void main() {
var isLoggedIn = true;
var items = <String>[];
print(isLoggedIn);
print(items.isEmpty);
print(!isLoggedIn);
print(5 > 3 && items.isNotEmpty);
}true
true
false
false
Checking types at runtime
The is operator tests a type, and runtimeType tells you the actual type of an object. After an is check, Dart promotes the variable so you can use that type's members.
void main() {
Object value = 'Flutter';
print(value.runtimeType);
print(value is String);
print(value is int);
if (value is String) {
// value is promoted to String here
print(value.toUpperCase());
}
}String
true
false
FLUTTER
Records
Dart 3 introduced records: lightweight, immutable groups of values. They are perfect for returning more than one value from a function without creating a class.
(String, int) person() => ('Linh', 29);
void main() {
var p = person();
print(p.$1);
print(p.$2);
var point = (x: 3, y: 4); // named fields
print('x=${point.x}, y=${point.y}');
var (name, age) = person(); // destructuring
print('$name is $age');
}Linh
29
x=3, y=4
Linh is 29
Positional fields are accessed with $1, $2, …; named fields use their names. You can unpack a record into separate variables with a pattern, as in var (name, age) = ....
Type aliases
typedef gives an existing type a shorter or more meaningful name:
typedef Json = Map<String, dynamic>;
typedef Point = ({double x, double y});Test yourself
What does 7 ~/ 2 evaluate to?
- 3.5
- 3
- 4
- 1
Which statement about Dart booleans is true?
if (1)is allowed- Empty strings are falsy
- Conditions must have type
bool nullcounts as false
Given var r = ('a', 2);, how do you read the number?
- r[1]
- r.2
- r.$2
- r.second