Skip to main content

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 TypeKeywordDescription
Numbersint, double, numIt represents the number of bits of the computer.
StringsStringIt represents a sequence of characters
BooleansboolIt represents Boolean values true and false
ListsListIt is an ordered group of items
MapsMapIt represents a set of values as key-value pairs
SetsSetIt is an unordered list of unique values of same types
RunesrunesIt represents Unicode values of String
NullnullIt 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 CharacterDescription
\n New Line
\tTab
\rCarriage Return
\bBackspace
\fForm Feed
\vVertical 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.