Table of contents
1.
Introduction
2.
Types of Data Types
2.1.
Number
2.2.
String
2.3.
Booleans
2.4.
Lists
2.5.
Map
2.6.
Set
3.
FAQs
3.1.
How is dart language helpful in making apps?
3.2.
What is the meaning of dynamic data types?
3.3.
Why can’t we declare the List without [] (square braces)?
3.4.
Why can’t we declare the map without {} (curly braces)?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Data Types in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Just like every other language (CC++Java), there is also the concept of data types in dart. To store a variable in a programming language, we use data types to specify the type of variable we are using. When we create a variable, we also provide some amount of memory to it. That amount of memory also varies for different data types. 

Dart also provides dynamic data types feature means when the variable type is not mentioned explicitly. We can use the dynamic data type by using the keyword dynamic.

Types of Data Types

There are several types of data types in the dart, such as:

Number

This data type is used to store numeric data like integer, double, etc. 

Keywords: int, double, num (inherited data type of int and double).

int: The int keyword in the Dart programming language represents an integer. Integers, as we all know, are made up of whole numbers (i.e., non-decimal positive and negative numbers). In the RAM, four bytes are assigned for each integer.

double: Double is a data type in the Dart programming language that stores decimal values (fractional numbers). Eight bytes are allocated in memory for each double variable. Any integer value you try to assign to the double variable will be cast to the double data type by default.

num: All numbers in Dart are part of the common Object type hierarchy, and there are two concrete, user-visible numeric types: int and double, which represent integer and fractional values, respectively. Those numeric types have distinct, hidden implementations depending on the platform. But now in new versions, you will not face any problems implementation-wise.
 

 

Dart Dev

 

Declaration and Initialization:

  int x;
  x = 100;
  double y;
  y = 10.5; 
  //We can also do both the step at once.
  int x = 100;
  double y = 10.5


Code:

void main() {
  // declare an integer
  int x = 100; 
  // declare a double value
  double y = 10.5;
  print(x);
  print(y);
  //passing string
  var x_new = num.parse("100");
  var y_new = num.parse("10.5");
  print(x_new);
  print(y_new);
}


Output:

100
10.5
100
10.5

String

The most common application of strings is to represent text. A string can be single or multiline in nature. Matching single or double quotes are used to write single line strings, and triple quotes are used to write multiline strings.

Keywords: String

Syntax:

  String s1 = 'Coding Ninjas';
  String s2 = "Coding Ninjas";
  String s3 = '''This is an example showing 
multiline string using single quotes.''';
  String s4 = """This is an example showing 
multiline string using double quotes.""";

//All above given strings are valid. 


Code:

void main() {
  String s1 = 'Coding';
  String s2 = " Ninjas";
  String s3 = s1 + s2;
  print(s3);
}


Output:

Coding Ninjas

Booleans

This data type is used to represent boolean type data mean either true or false.

Keywords: bool

Syntax:

  bool goodArticle = true;


Code:

void main() {
  bool goodArticle = true;
  print(goodArticle);
}


Output:

true

Lists

An array is a highly popular collection in programming. Arrays are represented in Dart as List objects. A list is essentially a collection of objects that are arranged in a certain order. The List class in the dart:core library allows you to create and manipulate lists.

Keywords: List

Syntax:

  List<int> l1 = [1,2,3];


Code:

void main() {
  List<int> l1 = [1,2,3];
  List<String> l2 = ['Ninja1','Ninja2'];
  print(l1);
  print(l2);
}


Output:

[1, 2, 3]
[Ninja1, Ninja2]

Map

Map is used to store the key-value pairs. Each key present in the map must be unique, whereas the values can occur multiple times. Every key is associated with only one value. We can declare a map data structure using curly braces {} and each entry is separated by a comma (,).

Keywords: Map

Syntax:

 Map<int,String> cn = {};


Code:

void main() {
  //Declaring a map
  Map<int,String> cn = {};
  //Inserting elements inside the map.
  cn[1] = "Coding";
  cn[2] = "Ninjas";
  cn[3] = "is";
  cn[4] = "good";
  print(cn);
}


Output:

{1: Coding, 2: Ninjas, 3: is, 4: good}

Set

Set is a data structure that contains a sequence of data having the same data types. Set consists of unique elements (duplicate elements are not allowed) and unordered in nature means it does not preserve the input order.

Keywords: Set

Syntax:

 Set<data_type> st = {};
//OR
var st = <data_type>{};


Code:

void main() 
{
  //Declaring and Initializing a set.
  Set<String> st = {"Pen","Paper","ball","Pen","Ink","Lense","Ball","Ink"};
  //Notice there are repeated elements in the set.
  print("Set: $st"); 
}


Output:

Set: {Pen, Paper, ball, Ink, Lense, Ball}

FAQs

How is dart language helpful in making apps?

Actually, we use flutter to make hybrid apps, and flutter is the framework of dart language. All the functionality of the flutter is based on the dart language. This means, that all the functions that we write will go to be in the dart.

 

What is the meaning of dynamic data types?

It means we can declare a variable and assign values of any data type to it. Dynamic is different from var because in var when we assign a value to a variable, it will become of that data type. Using dynamic keywords you can assign data of different data types to the variable.

 

Why can’t we declare the List without [] (square braces)?

Without [] we declare a null list, and we cannot add elements to the null list. With [] we declare an empty list, and we can add an element to an empty list.

 

Why can’t we declare the map without {} (curly braces)?

Without {} we declare a null map, and we cannot add elements to the null map. With {} we declare an empty map, and we can add an element to an empty map. 

Conclusion

In this article, we've thoroughly discussed the  Data Types in dart and the keywords we write to use them. We discussed how different data types could be declared and initialized.
We hope that this blog helped you enrich your knowledge regarding the Data Types in Dart. We can use the concepts of dart in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. 
Happy Coding!

Live masterclass