Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there are the types of values that can be represented and manipulated in a programming language.
In this article, we will learn about Dart Programming Language Data Types.
Data Types in Dart
The data type classification is as given below:
Data Type | Keyword | Description |
---|
Number | int, double, num, BigInt | Numbers in Dart are used to represent numeric literals |
Strings | String | Strings represent a sequence of characters |
Booleans | bool | It represents Boolean values true and false |
Lists | List | An ordered list of elements (like arrays in other languages) |
Sets | Set | A list of distinct elements (unsorted) |
Maps | Map | Collection of key-value pairs where keys are unique |
Runes | Runes | Used for manipulating Unicode characters (although characters API is now preferred) |
Symbols | Symbol | It is an identifier symbol, which is primarily utilized in reflection and debugging |
Null | Null | Represents the lack of a value |
1. Number (int, double, num, BigInt)
The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as:
- int: It is used to represent whole numbers (64-bit Max).
- double: It is used to represent 64-bit precise floating-point numbers.
- num: It is an inherited data type of the int and double types.
- BigInt: For very large integers that exceed
int
limits, use BigInt
.
Declaring Integer (int
)
// Method 1: Declaring an integer variable int age = 25; // Method 2: Nullable integer declaration int? count; // Method 3: Using 'var' keyword (automatically detects type) // Dart infers it as int var year = 2024;
Declaring Decimal Numbers (double
)
// Method 1: Explicit declaration double pi = 3.1415; // Method 2: Nullable double double? percentage; // Method 3: Using 'var' keyword // Dart infers it as double var temperature = 36.6;
Using num
for Both Integer and Decimal Values
num value = 10; // Allowed because 'num' supports both int and double value = 10.5;
Declaring Large Integers Using BigInt
BigInt bigNumber = BigInt.parse('987654321098765432109876543210');
Below is the implementation of Numbers in Dart:
Dart // Dart program to demonstrate // Number Data Type void main() { // Declare an integer int num1 = 2; // Declare a double value double num2 = 1.5; // Print the integer and // double values print("$num1"); print("$num2"); // Perform addition // (int + double results in a double) var sum = num1 + num2; // Print the sum of // num1 and num2 print("Sum = $sum"); }
Output:
2 1.5 Sum = 3.34
2. Strings (String )
It used to represent a sequence of characters. It is a sequence of UTF-16 code units. The keyword string is used to represent string literals. String values are embedded in either single or double-quotes.
Declaring String in Dart:
String str_name;
Below is the implementation of String data type in Dart:
Dart // Dart program to demonstrate // String Data Type void main() { // Declare and initialize a string String string = "Geeks for Geeks"; // Declare two separate string variables String str = 'Coding is '; String str1 = 'Fun'; // Print the string variable print(string); // Concatenate and print the two strings print(str + str1); // Output: Coding is Fun }
Output:
GeeksforGeeks Coding is Fun
3. Boolean (bool)
It represents Boolean values true and false. The keyword bool is used to represent a Boolean literal in dart.
Declaring Boolean in Dart
bool var_name;
Below is the implementation of Boolean in Dart:
Dart // Dart program to demonstrate // Boolean Data Type void main() { // Declare a boolean variable // with a true value bool val1 = true; // Declare two string variables String str = 'Coding is '; String str1 = 'Fun'; // Compare the two strings // (returns false) bool val2 = (str == str1); // Print boolean values print(val1); // Output: true print(val2); // Output: false }
Output:
true false
4. Lists (List)
List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects.
Declaring List in Dart
There are multiple methods to declare List in Dart as mentioned below:
1. Variable Size List
// Empty growable list (preferred way) List<int> var_name1 = []; // Alternative: Declaring a list but not initializing (null safety requires ? if uninitialized) List<int>? var_name2; // Another way using List constructor List<int> var_name3 = List.empty(growable: true);
2. Fixed Size List
Fixed Size doesn't mean that we can't change the size of List, but we have predefined that the List has this much elements while declaring.
// Creates a fixed-size list with default values (0 in this case) List<int> var_name1 = List<int>.filled(5, 0); // Generates a fixed-size list where each element is `index * 2` List<int> var_name2 = List<int>.generate(5, (index) => index * 2);
Below is the implementation of List in Dart:
Dart void main() { // Creating a fixed-size list with // 3 elements, each initialized to "default" List<String> gfg = List<String>.filled(3, "default"); // Updating elements in the list // (modifying values is allowed, // but resizing is not) gfg[0] = 'Geeks'; gfg[1] = 'For'; gfg[2] = 'Geeks'; // Printing the entire list to // verify the changes print(gfg); // Output: [Geeks, For, Geeks] // Accessing and printing a // specific element by its index print(gfg[0]); // Output: Geeks }
Output:
[Geeks, For, Geeks] Geeks
5. Sets (Set)
A Set
is an unordered collection of unique elements.
Declaring Set in Dart
// Method 1: Using curly braces Set<int> uniqueNumbers = {1, 2, 3, 3, 4}; // Method 2: Using Set constructor Set<String> cities = Set(); cities.add("New York"); cities.add("London");
Below is the implementation of Set in Dart:
Dart void main() { // Declaring a Set of Strings containing country names // Sets in Dart do not allow duplicate values Set<String> countries = {"USA", "India", "USA"}; // Printing the set // Since sets store only unique values, "USA" appears only once print(countries); // Output: {USA, India} }
Output:
{USA, India}
6. Maps (Map)
The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection.
Declaring Map in Dart
While Declaring Map there can be only two cases one where declared Map is empty and another where declared Map contains elements in it. Both Cases are mentioned below:
1. Declaring Empty Map
// Method 1: Nullable Map declaration (can be assigned later) Map? mapName; // Method 2: Explicitly specifying key-value data types Map<String, int>? mapName2; // Method 3: Using the 'var' keyword with Map constructor // Defaults to Map<dynamic, dynamic> var mapName3 = Map(); // Method 4: Using a type-safe empty map
// Recommended approach
Map<String, int> mapName4 = {};
2. Declaring Map with Elements inside it.
/// Method 1: Using curly braces (preferred way) Map<String, String> myMap = { "First": "Geeks", "Second": "For", "Third": "Geeks", }; // Method 2: Using Map constructor Map<String, int> mapExample = Map(); mapExample["One"] = 1; mapExample["Two"] = 2; // Method 3: Using 'var' with implicit typing var anotherMap = { "A": 10, "B": 20, "C": 30, };
Below is the implementation of Map in Dart:
Dart void main() { // Creating a map using the preferred syntax Map<String, String> gfg = {}; // Adding key-value pairs gfg['First'] = 'Geeks'; gfg['Second'] = 'For'; gfg['Third'] = 'Geeks'; // Printing the map print(gfg); }
Output:
{First: Geeks, Second: For, Third: Geeks}
7. Runes (Runes
)
Dart utilizes Runes to represent Unicode characters that fall outside the standard ASCII character set. Since Dart strings are encoded in UTF-16, certain special characters such as emojis and non-English scripts must be expressed using runes.
Declaring Runes in Dart
String heart = '\u2665'; // Unicode for ♥
Below is the implementation of Runes in Dart:
Dart void main() { // Unicode for heart symbol (♥) String heart = '\u2665'; // Unicode for smiley face (☺) String smiley = '\u263A'; // Unicode for star symbol (★) String star = '\u2605'; // Unicode for musical note (♫) String musicNote = '\u266B'; // Printing all Unicode symbols print(heart); // Output: ♥ print(smiley); // Output: ☺ print(star); // Output: ★ print(musicNote); // Output: ♫ }
Output:
♥ ☺ ★ ♫
8. Symbols (Symbol
)
A Symbol in Dart is an immutable identifier that represents variable names, method names, or metadata at runtime. Symbols are primarily used in reflection and are helpful for dynamic programming.
Declaring Symbols in Dart
Symbol sym1 = #mySymbol; Symbol sym2 = Symbol("anotherSymbol");
Below is the implementation of Symbols in Dart:
Dart void main() { // Declaring a Symbol // using the # syntax Symbol sym1 = #dart; // Declaring another Symbol // with a different identifier Symbol sym2 = #flutter; // Printing the Symbols print(sym1); // Output: Symbol("dart") print(sym2); // Output: Symbol("flutter") // Using Symbol in a map // (useful for metadata or reflection) Map<Symbol, String> symbolMap = { #language: "Dart", #framework: "Flutter", }; // Printing values using Symbols as keys print(symbolMap[#language]); // Output: Dart print(symbolMap[#framework]); // Output: Flutter }
Output:
Symbol("dart") Symbol("flutter") Dart Flutter
9. Null (Null
)
Dart uses null to indicate the absence of a value. It has null safety, which means variables must be either nullable (?) or initialized before they can be used.
Declaring Null in Dart
String? name; // Can be null
Below is the implementation of Null in Dart:
Dart void main() { // Nullable variables (can be assigned null) String? name; int? age; // Assigning values name = "GFG"; age = null; // Checking for null values // using null-aware operators print(name ?? "Unknown"); // Output: Vinay print(age ?? "No age provided"); // Output: No age provided // Using null-aware access (?.) // and null assertion (!) int? length = name?.length; print(length); // Output: 5 }
Output:
GFG No age provided 3
Note: If the type of a variable is not specified, the variable’s type is dynamic. The dynamic keyword is used as a type annotation explicitly.
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts
My Profile