In this tutorial you will learn about the Dart Data Types and its application with practical example.
Dart Data Types
Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose to allocate some memory space for that variable. Dart is a statically typed programming language. This means that variables always have a specific type and that type cannot change. Every variable have data type associated to it, data type for a variable defines –
- The amount of memory space allocated for variables.
- A data type specifies the possible values for variables.
- The operations that can be performed on variables.
Note :- Although Dart is strongly typed, type annotations are optional because Dart can infer types.
Dart has following built-in data types –
- Numbers
- Strings
- Boolean
- Lists
- Maps
- Runes
- Symbols
Dart Numbers:- The Number data type is used to hold the numeric values. Dart supports following numerical data types –
- Integer
- Double
Dart Integer:- Integers are used to store whole numbers. An integer data type is used to represent 64 bit non-decimal number between -263 to 263 – 1. An integer can be used to store either signed and unsigned integer value. Integers can be declared using int keyword.
1 |
int age = 25; |
Dart Double :- In Dart, double is used to represent a 64-bit (double-precision) floating-point numbers or numbers with larger decimal points. Double can be declared using double keyword.
1 |
double pi = 3.14; |
Dart Strings :- A string variable is used to hold series or sequence of characters – letters, numbers, and special characters. In Dart, string can be represented either using single quotes or double quotes. In Dart, strings can be declared using the String keyword.
1 |
Var msg="Hello World!"; |
Dart Boolean :- The Boolean data type is used to represent the truth values, which can be either True or False. Boolean are commonly used in decision making statements. In Dart, you cannot use 0 or 1 to represent true or false. Boolean can be declared using bool keyword.
1 |
bool isValid = true; |
Dart Lists :- In Dart, list data type is used to represent a collection of objects. A List is an ordered group of objects. The List data type in Dart is synonymous to the concept of an array in other programming languages. An array is used to hold multiple values in single variable. In Dart, arrays are List objects, so most people just call them lists. Dart list literals look like JavaScript array literals. A list variable is defined by having values separated by commas and enclosed within square brackets ([]).
1 |
var list = [1, 2, 3]; |
Dart Maps :- The Map is an object that is used to represents a set of values as key-value pairs. In Map, both keys and values can be of any type of object. In Map, each key can only occurs once, but the same value can be used multiple times. The Map can be defined by using curly braces ({ }) and values can be assigned and accessed using square braces ([]).
1 |
var weekDays = {'Day1': 'Mon', 'Day2': 'Tue', 'Day3': 'Wed', 'Day4': 'Thu'}; |
Dart Runes :- Dart string is a sequence of Unicode UTF-16 code units, 32-bit Unicode values within a string are represented using a special syntax. A rune is an integer representing a Unicode code point. For example, the heart character ‘♥ is represented using corresponding unicode equivalent \u2665, here \u stands for unicode and the numbers are hexadecimal, which is essentially an integer. If the hex digits are more or less than 4 digits, place the hex value in curly brackets ({ }). For example, the laughing emoji ‘😆’ is represented as \u{1f600}.
Example:-
1 2 3 4 5 6 7 |
void main() { var heart = '\u2665'; var laugh = '\u{1f600}'; print(heart); print(laugh); } |
Output:-
1 2 |
♥ 😀 |
Dart Symbols :- Dart Symbol object used to refer an operator or identifier declared in a Dart program. Dart symbol are commonly used in APIs that refer to identifiers by name, because an identifier name can changes but not identifier symbols. Symbol for an identifier can be created using a hash (#) followed by the identifier name.
1 2 |
#radix #bar |
Dart Dynamic Type
Dart is an optionally typed language. If the type of a variable is not explicitly specified, the variable’s type is dynamic. The dynamic keyword can also be used as a type annotation explicitly.