Introduction
Dart is a statically typed language, which means every variable has a type. Understanding data types is essential for working effectively with Dart. This guide covers the five fundamental data types you’ll use most often.The Five Core Data Types
int
Integer numbers (whole numbers without decimals)
double
Decimal numbers (floating-point numbers)
String
Text and character sequences
bool
Boolean values (true or false)
dynamic
Dynamic type (can change at runtime)
int - Integer Numbers
Theint type represents whole numbers without decimal points. They can be positive, negative, or zero.
In Dart, integers can be very large. The exact range depends on the platform, but Dart handles large numbers efficiently.
double - Decimal Numbers
Thedouble type represents floating-point numbers (numbers with decimal points).
String - Text Data
TheString type represents sequences of characters (text). Strings are enclosed in either single quotes '...' or double quotes "...".
String Characteristics
- Can contain any characters: letters, numbers, symbols, spaces
- Can be empty:
'' - Enclosed in single
'...'or double"..."quotes - Numbers in quotes are treated as text, not numeric values
bool - Boolean Values
Thebool type represents logical values: true or false. Booleans are essential for conditional logic and decision-making in programs.
Boolean values are always lowercase in Dart:
true and false (not True or FALSE).dynamic - Dynamic Type
Thedynamic type is special - it can hold any type of value and can change its type at runtime.
When to Use dynamic
- Working with data from external sources where the type isn’t known at compile time
- Interfacing with JavaScript or other dynamic languages
- Handling JSON data before parsing
Complete Example
Here’s a comprehensive example using all five data types:Type Summary Table
| Type | Description | Example Values |
|---|---|---|
int | Whole numbers | 10, -5, 0, 1000000 |
double | Decimal numbers | 3.14, 0.5, 19.0, -2.5 |
String | Text | 'Hello', "Dart", '123', '' |
bool | Boolean | true, false |
dynamic | Any type | Can be any of the above |
Key Takeaways
- int: Use for counting, indexing, and whole number calculations
- double: Use for measurements, percentages, and precise calculations
- String: Use for text, names, messages, and character data
- bool: Use for flags, conditions, and true/false states
- dynamic: Use sparingly when type is truly unknown at compile time