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.
double: Represents numbers with a decimal point (floating-point numbers).
Dart also has the num
type, which is a superclass for both int
and double
, allowing you to store either type:
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.
Multiline Strings: Use triple quotes (
'''
or"""
) for strings that span multiple lines.String Interpolation: Dart supports embedding variables directly in strings using
${variable}
syntax. For simple variables, you can omit the curly braces.
3. Booleans
The bool
data type in Dart is used to represent logical values true
or false
. Booleans are commonly used in conditional statements.
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:
Adding and Removing Elements:
List Literal Shortcut: Dart also allows declaring lists without specifying the
List
type explicitly:
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:
Adding and Removing Elements:
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:
Accessing Elements:
Adding and Removing Elements:
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:
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.
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.Object: The
Object
type is the root of the Dart type hierarchy. All types inherit fromObject
. Unlikedynamic
, anObject
variable must be explicitly cast to its type to use specific properties or methods.
Summary of Dart Data Types
Data Type | Description | Example |
---|---|---|
int | Whole numbers | int age = 25; |
double | Decimal numbers | double pi = 3.14; |
String | Text or character sequences | String name = 'Alice'; |
bool | Boolean values (true/false) | bool isActive = true; |
List | Ordered collection of items | List<String> fruits = ['apple']; |
Set | Collection of unique items | Set<int> ids = {1, 2, 3}; |
Map | Collection of key-value pairs | Map<String, int> ages = {...}; |
Runes | Unicode characters | String emoji = '\u{1F600}'; |
Symbol | Identifier for names | Symbol sym = #name; |
dynamic | Variable that can hold any type (runtime) | dynamic var = "Hello"; |
Object | Base type of all Dart objects | Object 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.