Skip to main content

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

The int 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

The double type represents floating-point numbers (numbers with decimal points).
Even if a number doesn’t have a fractional part (like 19.0), including the decimal point makes it a double rather than an int.

String - Text Data

The String type represents sequences of characters (text). Strings are enclosed in either single quotes '...' or double quotes "...".
'56' is a String, not a number. You cannot perform mathematical operations on it without first converting it to a number.

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

The bool 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

The dynamic type is special - it can hold any type of value and can change its type at runtime.
Use dynamic sparingly! While flexible, it bypasses Dart’s type safety and can lead to runtime errors. Prefer specific types when possible.

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
In most cases, you should prefer specific types (int, String, etc.) over dynamic to leverage Dart’s type safety features.

Complete Example

Here’s a comprehensive example using all five data types:

Type Summary Table

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

Next Steps

Now that you understand Dart’s basic data types, you’re ready to learn about constants and how to create values that never change.