TExcelTAD
Home › Dart › Dart Built-in Types

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:

dart · try it yourself
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: runtimeType may show an internal implementation class such as _Map. Use is checks in real code, not runtimeType comparisons.

The type hierarchy

text
            Object?
           /       \
       Object      Null
     /   |   \  \
   num String bool List, Set, Map, Record, your classes…
   /  \
 int  double
  • Object is the root of all non-nullable types.
  • Object? (or dynamic) can hold anything, including null.
  • Never is 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
dart · try it yourself
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:

dart · try it yourself
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:

dart · try it yourself
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?

  1. Number
  2. num
  3. float
  4. Object only

What does int.tryParse('abc') return?

  1. 0
  2. -1
  3. null
  4. It throws FormatException

What does x is! String test?

  1. x is a String
  2. x is not a String
  3. x is a nullable String
  4. It casts x