Dart Data Types
Dart Data Types
Every value in Dart has a data type.
The data type decides what kind of value it is and what you can do with it.
For example, you can multiply two numbers, but you cannot multiply two names.
Example
void main() {
int age = 30;
double price = 9.99;
String name = 'Emma';
bool isMember = true;
print(age);
print(price);
print(name);
print(isMember);
}
Try it Yourself »
Result:
30
9.99
Emma
true
Built-in Data Types
This table shows the most important data types in Dart:
| Type | Description | Example |
|---|---|---|
int | Whole numbers, without decimals | 25, -7 |
double | Numbers with decimals | 3.14, -0.5 |
num | Any number, an int or a double | 10, 10.5 |
String | Text | 'Hello' |
bool | true or false | true |
List | An ordered collection of values | ['Volvo', 'BMW'] |
Set | A collection of unique values | {'red', 'green'} |
Map | A collection of key and value pairs | {'name': 'Emma', 'city': 'Oslo'} |
Record | A small, fixed group of values | ('Emma', 30) |
dynamic | Turns off type checks, so the variable can hold any type | dynamic x = 5; |
Object | Any value that is not null | Object x = 'Hi'; |
You will learn more about these types in later chapters.
Get the Type With runtimeType
You can find the type of any value with the runtimeType property:
Example
void main() {
var a = 25;
var b = 3.14;
var c = 'Hello';
var d = true;
var e = ['Volvo', 'BMW'];
print(a.runtimeType);
print(b.runtimeType);
print(c.runtimeType);
print(d.runtimeType);
print(e.runtimeType);
}
Try it Yourself »
Result:
int
double
String
bool
List<String>
The type List<String> means "a list of strings".
Note: Use runtimeType to learn and to find mistakes.
To check a type in a real program, use the is operator, which you will learn in the Type Test chapter.
Numbers, Text and Booleans
int and double are both numbers, and num can hold either of them:
Example
void main() {
num distance = 10;
print(distance);
distance = 10.5;
print(distance);
}
Try it Yourself »
Result:
10
10.5
Text is stored as a String, and a yes or no value is stored as a bool:
Example
void main() {
String greeting = 'Good morning';
bool isSunny = false;
print(greeting);
print(isSunny);
}
Try it Yourself »
Result:
Good morning
false
Read more in Numbers, Strings and Booleans.
Collections
A collection is a type that holds many values.
A List keeps values in order, a Set keeps only unique values, and a Map stores pairs of keys and values:
Example
void main() {
var cars = ['Volvo', 'BMW', 'Ford'];
var colors = {'red', 'green', 'red'};
var person = {'name': 'Emma', 'city': 'Oslo'};
print(cars);
print(colors);
print(person);
}
Try it Yourself »
Result:
[Volvo, BMW, Ford]
{red, green}
{name: Emma, city: Oslo}
Note that the set prints red only once.
Read more in Lists, Sets, Maps and Records.
dynamic and Object
A variable of type dynamic or Object can hold a value of any type:
Example
void main() {
dynamic x = 5;
print(x);
x = 'Hello';
print(x);
Object y = 5;
print(y);
y = true;
print(y);
}
Try it Yourself »
Result:
5
Hello
5
true
The difference is that Dart does not check what you do with a dynamic value.
A mistake with a dynamic value is only found when the program runs:
Result:
Start
Unhandled exception:
NoSuchMethodError: Class 'int' has no instance method 'toUpperCase'.
Receiver: 5
Tried calling: toUpperCase()
With Object, Dart finds the same mistake before the program runs:
Result:
main.dart:4:11: Error: The method 'toUpperCase' isn't defined for the type 'Object'.
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'toUpperCase'.
print(x.toUpperCase());
^^^^^^^^^^^
Tip: Use a real type like int or String when you can.
Use dynamic only when you really do not know the type.