Dart Built-in Types
Every value in Dart is an object, and every object has a type. Dart's core library (dart:core, imported automatically) defines a small set of built-in types that you will use in almost every program.
Overview
| Type | Example literal | Notes |
|---|---|---|
int |
42, 0xFF, 1_000_000 |
Whole numbers, 64-bit on native platforms |
double |
3.14, 1e-3 |
64-bit floating point |
num |
— | Supertype of int and double |
String |
'hi', "hi", '''multi''' |
UTF-16 text, immutable |
bool |
true, false |
Only these two values |
List<T> |
[1, 2, 3] |
Ordered, indexable collection |
Set<T> |
{'a', 'b'} |
Unique elements |
Map<K, V> |
{'id': 1} |
Key–value pairs |
Record |
(1, 'one'), (x: 1, y: 2) |
Fixed-size, anonymous, typed tuples |
Runes |
'😀'.runes |
Unicode code points of a string |
Symbol |
#name |
Rarely used; for reflection-style APIs |
Null |
null |
The type of null |
Inspecting types at runtime
Every object has a runtimeType and you can test types with is:
void main() {
var values = [7, 2.5, 'text', true, [1], {'k': 'v'}, (1, 2), null];
for (final v in values) {
print('$v -> ${v.runtimeType}');
}
}7 -> int
2.5 -> double
text -> String
true -> bool
[1] -> List<int>
{k: v} -> _Map<String, String>
(1, 2) -> (int, int)
null -> Null
Note:
runtimeTypemay show an internal implementation class such as_Map. Useischecks in real code, notruntimeTypecomparisons.
The type hierarchy
Object?
/ \
Object Null
/ | \ \
num String bool List, Set, Map, Record, your classes…
/ \
int doubleObjectis the root of all non-nullable types.Object?(ordynamic) can hold anything, includingnull.Neveris the type of expressions that never complete (e.g.throw).
Type tests and casts
| Operator | Meaning |
|---|---|
x is T |
true if x is a T |
x is! T |
true if x is not a T |
x as T |
Cast — throws if x is not a T |
void describe(Object value) {
if (value is int) {
// value is promoted to int inside this block
print('int, doubled: ${value * 2}');
} else if (value is String) {
print('String of length ${value.length}');
} else if (value is! num) {
print('something else: $value');
}
}
void main() {
describe(21);
describe('Dart');
describe(true);
Object o = 'hello';
var s = o as String;
print(s.toUpperCase());
}int, doubled: 42
String of length 4
something else: true
HELLO
After value is int, Dart promotes value to int in that branch, so you can call integer operations without a cast.
Type conversions
Dart never converts types implicitly — 'Total: ' + 5 is an error. Convert explicitly:
void main() {
int a = int.parse('42');
double b = double.parse('3.5');
int? bad = int.tryParse('4x2'); // null instead of an exception
String fromInt = 42.toString();
String fixed = 3.14159.toStringAsFixed(2);
int truncated = 9.99.toInt();
double widened = 5.toDouble();
print([a, b, bad, fromInt, fixed, truncated, widened]);
}[42, 3.5, null, 42, 3.14, 9, 5.0]
Typedefs
A typedef gives a name to any type, which helps with long function or generic types:
typedef Scores = Map<String, List<int>>;
typedef Validator = bool Function(String);
void main() {
Scores scores = {'Lan': [8, 9], 'Minh': [7]};
Validator notEmpty = (s) => s.isNotEmpty;
print(scores['Lan']);
print(notEmpty(''));
}[8, 9]
false
Test yourself
Which type is the common supertype of int and double?
- Number
- num
- float
- Object only
What does int.tryParse('abc') return?
- 0
- -1
- null
- It throws FormatException
What does x is! String test?
- x is a String
- x is not a String
- x is a nullable String
- It casts x