Dart Data Types


Dart provides a range of data types to handle various types of data, from simple numbers to complex collections. The language has both primitive data types (like integers, doubles, strings, and booleans) and complex types (like lists, sets, and maps). Understanding these data types helps you effectively manage and manipulate data in Dart.


1. Numbers

Dart provides two primary data types for numbers: int and double.

  • int: Represents whole numbers (without a decimal point) and is used for integers.

    int age = 25; int year = 2024;
  • double: Represents numbers with a decimal point (floating-point numbers).

    double height = 5.9; double price = 19.99;

Dart also has the num type, which is a superclass for both int and double, allowing you to store either type:

num temperature = 98.6; // Can be double or int temperature = 37; // Changing to int is valid

2. Strings

Strings are sequences of characters, typically used for text. Dart provides several ways to create and manipulate strings.

  • Single or Double Quotes: Strings can be enclosed in either single or double quotes.

    String single = 'Hello'; String double = "World";
  • Multiline Strings: Use triple quotes (''' or """) for strings that span multiple lines.

    String multiline = '''This is a multiline string.''';
  • String Interpolation: Dart supports embedding variables directly in strings using ${variable} syntax. For simple variables, you can omit the curly braces.

    String name = 'Alice'; print('Hello, $name'); // Output: Hello, Alice print('2 + 2 = ${2 + 2}'); // Output: 2 + 2 = 4

3. Booleans

The bool data type in Dart is used to represent logical values true or false. Booleans are commonly used in conditional statements.

bool isActive = true; bool isVerified = false;

4. Lists

Lists in Dart are ordered collections of items, similar to arrays in other languages. They are zero-indexed, meaning the first item has an index of 0. Dart lists can be either fixed-length or growable.

  • Creating a List:

    List<String> colors = ['red', 'green', 'blue']; print(colors[0]); // Output: red
  • Adding and Removing Elements:

    colors.add('yellow'); // Adds an element to the list colors.remove('green'); // Removes the element 'green'
  • List Literal Shortcut: Dart also allows declaring lists without specifying the List type explicitly:

    var numbers = [1, 2, 3, 4]; // List<int>

5. Sets

A Set is a collection of unique items and is useful when you don’t want duplicates. Sets in Dart are unordered, meaning the order of items isn’t guaranteed.

  • Creating a Set:

    Set<int> uniqueNumbers = {1, 2, 3, 4, 4}; print(uniqueNumbers); // Output: {1, 2, 3, 4} (duplicates are removed)
  • Adding and Removing Elements:

    uniqueNumbers.add(5); // Adds 5 to the set uniqueNumbers.remove(2); // Removes 2 from the set

6. Maps

A Map in Dart is a collection of key-value pairs. Each key is unique, and it maps to a value. Maps are unordered, like sets, but can be accessed by their keys.

  • Creating a Map:

    Map<String, int> scores = { 'Alice': 95, 'Bob': 85, 'Charlie': 90 };
  • Accessing Elements:

    print(scores['Alice']); // Output: 95
  • Adding and Removing Elements:

    scores['David'] = 80; // Adds a new key-value pair scores.remove('Bob'); // Removes the key 'Bob' and its value

7. Runes

Runes are used to represent Unicode characters. In Dart, you can use runes to work with special characters or emojis.

  • Creating a Runes String:
    String smiley = '\u{1F600}'; print(smiley); // Output: 😀

8. Symbols

Symbols represent an identifier that can be used to refer to a name or member in Dart. They are rarely used but can be useful when working with reflection or dynamically accessing members.

Symbol symbol = #someIdentifier;

9. Dynamic and Object Types

Dart allows dynamic typing using the dynamic and Object types.

  • dynamic: A variable declared as dynamic can hold any type of data, and its type can change at runtime. This is useful for cases where you don't know the type of data in advance.

    dynamic variable = "Hello"; variable = 42; // Allowed
  • Object: The Object type is the root of the Dart type hierarchy. All types inherit from Object. Unlike dynamic, an Object variable must be explicitly cast to its type to use specific properties or methods.

    Object obj = 'Dart'; print((obj as String).length); // Typecasting to String

Summary of Dart Data Types

Data TypeDescriptionExample
intWhole numbersint age = 25;
doubleDecimal numbersdouble pi = 3.14;
StringText or character sequencesString name = 'Alice';
boolBoolean values (true/false)bool isActive = true;
ListOrdered collection of itemsList<String> fruits = ['apple'];
SetCollection of unique itemsSet<int> ids = {1, 2, 3};
MapCollection of key-value pairsMap<String, int> ages = {...};
RunesUnicode charactersString emoji = '\u{1F600}';
SymbolIdentifier for namesSymbol sym = #name;
dynamicVariable that can hold any type (runtime)dynamic var = "Hello";
ObjectBase type of all Dart objectsObject obj = 5;

Understanding these types helps you select the most appropriate one for storing data, improving code readability and efficiency. Dart’s flexible typing system also provides dynamic and Object types for cases requiring more flexibility.