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.
void main() {
  // int: Whole numbers
  int numero = 10;
  int edad = 18;
  int entero = -1223343553445632345;
  
  print(numero);  // 10
  print(edad);    // 18
  print(entero);  // -1223343553445632345
}
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).
void main() {
  // double: Decimal numbers
  double promedio = 9.4;
  double valor = 0.50;
  double numeroDos = 19.0;
  double doble = 1265464764675476465456.2342121216571321315215341345234532453;
  
  print(promedio);   // 9.4
  print(numeroDos);  // 19.0
  print(valor);      // 0.5
  print(doble);      // 1.2654647646754765e+21
}
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 "...".
void main() {
  // String: Text sequences
  String cadena = 'Cualquier texto es valido entre comillas';
  String sexo = 'Masculino';
  String numeroCadena = '56';  // This is a STRING, not a number!
  String espacio = ' ';
  String nulo = '';            // Empty string
  
  print(cadena);        // Cualquier texto es valido entre comillas
  print(sexo);          // Masculino
  print(nulo);          // (empty)
  print(espacio);       // (space)
  print(numeroCadena);  // 56
}
'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.
void main() {
  // bool: True or False values
  bool esEstudiante = true;
  bool esMayorEdad = false;
  
  print(esEstudiante);  // true
  print(esMayorEdad);   // false
}
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.
void main() {
  // dynamic: Can change type at runtime
  dynamic valorDinamico = 10;                      // int
  valorDinamico = 'Puedo cambiar a String';       // now String
  valorDinamico = false;                          // now bool
  valorDinamico = 8.3;                            // now double
  
  print(valorDinamico);  // 8.3
  
  valorDinamico = 'Hola';
  print(valorDinamico);  // Hola
}
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:
void main() {
  // Integer
  int edad = 25;
  
  // Double
  double salario = 45000.50;
  
  // String
  String nombre = 'Ana García';
  
  // Boolean
  bool esEmpleado = true;
  
  // Dynamic
  dynamic informacionExtra = 'Departamento: IT';
  
  // Display employee information
  print('Nombre: $nombre');
  print('Edad: $edad');
  print('Salario: \$${salario}');
  print('Es empleado: $esEmpleado');
  print(informacionExtra);
  
  // Dynamic can change
  informacionExtra = 5;  // Now it's a number
  print('Años de experiencia: $informacionExtra');
}

Type Summary Table

TypeDescriptionExample Values
intWhole numbers10, -5, 0, 1000000
doubleDecimal numbers3.14, 0.5, 19.0, -2.5
StringText'Hello', "Dart", '123', ''
boolBooleantrue, false
dynamicAny typeCan 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

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.

Build docs developers (and LLMs) love