Different Data Types in Dart

  • Post author:
  • Post comments:0 Comments
  • Reading time:65 mins read

When writing Dart code, it’s essential to have a solid understanding of the various data types available and how to use them effectively. In this comprehensive guide, we will explore the different data types in Dart, ranging from numbers and strings to collections like lists and maps. We will also delve into the usage and characteristics of each data type, along with practical examples to reinforce your understanding

Data Types in Dart

1) int to store integer numbers
2) double to store decimal numbers
3) num It represents all kinds of numbers, including integers and floating-point numbers
4) String to store letters or words string must be represented like a String
5) bool can only take 2 values true or false
6) var can be taken from any data type mentioned above
7) dynamic it can store any value as var
8) List
9) Map

int Data Type

int: In Dart, the int the data type is used to store whole numbers. It represents integers without any fractional or decimal parts. You can declare a int variable and assign it a value like int age = 25;. The variable age will store the value 25.

Let’s look at the simple code that represents how to store integer values and print them to the user

void main(){
  int age = 25;

double Data Type in dart

double: The double data type in Dart is used to store decimal numbers. It represents numbers with fractional parts. For example, you can declare a double variable like double price = 9.99;. The variable price will store the value of 9.99.

The double data type is represented using double built-in keyword now let’s look at a simple program that represents how to write this code

void main(){

  double price = 25.5;

num Data type in Dart

In Dart, the num data type is a superclass of both int and double. It represents all kinds of numbers, including integers and floating-point numbers. The num type is useful in situations where you want to work with both whole numbers and decimal numbers without explicitly specifying whether the value will be an int or a double.

Here are some examples of how to use the num data type in dart

void main() {

  num length = 5;
  num width = 3.5;

String data type in Dart

Above we have a look at the data type that stores numbers and decimal points now let’s learn how to store a sequence of characters. In Dart, the String Data type is used to store and manipulate textual data, such as names, messages, or any other piece of text. Strings are immutable, meaning their values cannot be changed once they are created.

Note that In Dart, the String data type starts with a capital letter. It is considered a built-in type, and its name is capitalized to follow Dart’s naming conventions

Now let’s create a simple code explaining this Data type

void main() {

  String greeting = "Hello,";
  String name = "John";

  String message = greeting + " " + name + "!";


Bool data type in dart

A bool data type is the data type in Dart that can only take two values true or false. Booleans are used to represent logical conditions and make decisions based on those conditions in programming. To declare a bool variable, you use the bool keyword followed by the variable name and an optional initial value assigned using the = operator. For example

void main() {

  bool isRaining = true;
  bool isSunny = false;

  print('Is it raining? $isRaining');
  print('Is it sunny? $isSunny');



In this code, we declare two bool variables: isRaining and isSunny. The variable isRaining represents whether it is currently raining (true), and isSunny represents whether it is sunny (false).

We then use the print() function to display the values of the boolean variables. The string interpolation ($variable) is used to embed the values of the boolean variables within the printed statements.

Var Data type in Dart

The var keyword is used for declaring variables without specifying their types explicitly. Dart infers the type based on the value assigned to the variable. The type of a var variable can change at runtime based on the assigned value. For example:

void main() {
  // var
  var age = 25;
  var name = "John";
  print("Age: $age, Name: $name");

The var keyword is used to declare age and name variables, with their types inferred by Dart based on the assigned values.

dynamic Data type in Dart

The dynamic keyword is used for declaring variables that can hold values of any type. It allows you to bypass type checks during compile-time, but still performs runtime type checking. This flexibility comes at the cost of potentially losing some of the benefits of static typing. For example:

void main(){

  dynamic value = 10;
  print("dynamic - Value: $value");
  value = "Hello";
  print("dynamic - Value: $value");

The dynamic keyword is used to declare the value variable, which can hold values of any type.

List Data type in Dart

The List data type represents an ordered collection of objects of the same type. It is similar to an array in other programming languages. Lists in Dart are zero-indexed, meaning the first element is at index 0. You can add, modify, or remove elements from a list. For example:

void main(){

List<int> numbers = [1, 2, 3, 4];
numbers[2] = 10;


Here after List we declare that <int> beacuse that we tell the programming language that the list item is containing integer numbers. If you want to store string in List then after List data type declare as <String> the this will understand that user is trying to add data that contains sequence of characters.

Map Data type in Dart

The Map data type represents a collection of key-value pairs. Each key in a map must be unique, and the keys and values can be of any type. Maps are sometimes referred to as dictionaries or hash tables. For example:

void main(){

Map<String, int> ages = {
  "John": 25,
  "Alice": 30,
  "Bob": 35,
ages["Alice"] = 31;


In the above code, we declare a map named ages that associates names (strings) with ages (integers). We can access, modify, or remove values from the map using the keys.

Understanding these data types in Dart allows you to effectively work with variables of different types, store collections of values, and perform various operations on them.



Publisher @ideasorblogs

Leave a Reply