Data Types in Dart
Data types help you to categorize all the different types of data you use in your code. For e.g. numbers, texts, symbols, etc. The data type specifies what type of value will be stored by the variable. Each variable has its data type. Dart supports the following built-in data types :
- Numbers
- Strings
- Booleans
- Lists
- Maps
- Sets
- Runes
- Null
Built-In Typesβ
In Dart language, there is the type of values that can be represented and manipulated. The data type classification is as given below:
Data Type | Keyword | Description |
---|---|---|
Numbers | int, double, num | It represents the number of bits of the computer. |
Strings | String | It represents a sequence of characters |
Booleans | bool | It represents Boolean values true and false |
Lists | List | It is an ordered group of items |
Maps | Map | It represents a set of values as key-value pairs |
Sets | Set | It is an unordered list of unique values of same types |
Runes | runes | It represents Unicode values of String |
Null | null | It represents null value |
Numbersβ
When you need to store numeric value on dart, you can use either int or double. Both int and double are subtype of num. You can use num to store both int or double value.
Round Double Value To 2 Decimal Placesβ
The .toStringAsFixed(2)
is used to round the double value upto 2 decimal places in dart. You can round to any decimal
places by entering number like 2, 3, 4.
Stringβ
String helps you to store text data. You can store values like I love dart, New York 2140 in String. You can use single or double quotes to store string in dart.
Create A Multi-Line String In Dartβ
If you want to create a multi-line String in dart, then you can use triple quote with either single or double quotation marks.
Special Character In String In Dartβ
Special Character | Description |
---|---|
\n | New Line |
\t | Tab |
\r | Carriage Return |
\b | Backspace |
\f | Form Feed |
\v | Vertical Tab |
\' | Single Quote |
\" | Double Quote |
\\ | Backslash |
Exampleβ
Create A Raw String In Dartβ
You can also create raw string in dart. Special characters wonβt work here. You must write r
after equal sign.
Type Conversion In Dartβ
In dart, type conversion allows you to convert one data type to other type. For e.g. to convert String
to int
, int
to
String
or String
to bool
, etc.
Convert String To Int In dartβ
You can convert String to int using int.parse()
method. The method takes String as an argument and converts it into an
integer.
Convert Int To String In Dartβ
You can convert int to String using toString()
method. Here is example:
Convert Double To Int In Dartβ
You can convert double to int using toInt()
method.
Booleansβ
In Dart, boolean holds either true or false value. You can write bool
keyword to define boolean
data type.
Listsβ
The list holds multiple values in a single variable. It is also called arrays. If you want to store multiple values without creating multiple variables, you can use a list.
Note
List index always starts with 0. Here names[0]
is Raj, names[1]
is John and names[2]
is Max.
Setsβ
An unordered collection of unique items is called set in dart. You can store unique data in sets.
Note
Set doesβt print duplicate items.
Mapsβ
In dart, a map
is an object where you can store data in key-value pairs. Each key occurs only once, but you
can use same
value multiple times.
Var Keyword In Dartβ
In dart, var
automatically finds a data type. In simple terms, var
says if you donβt want to specify a data
type, I will find a data type for you.
Runes In Dartβ
With runes, you can find Unicode values of String
. The Unicode value of a is 97, so runes give 97 as output.
How To Check Runtime Typeβ
You can check runtime type in dart with .runtimeType
after variable name.
Optionally Typed Languageβ
You may be heard of the statically-typed language. It means the data type of variables is known at compile time. Similarly, dynamically-typed language means data types of variables are known at run time. Dart supports dynamic and static types, so it is called optionally-typed language.
Statically Typedβ
A language is statically typed if the data type of variables is known at compile time. Its main advantage is that the compiler can quickly check the issues and detect bugs.
Dynamically Typed Exampleβ
A language is dynamically typed if the data type of variables is known at run time.
Note
Using static type helps you to prevent writing silly mistakes in code. Itβs a good habit to use static type in dart.