Dart Programming Part-3: Data Types

Jayesh Pansheriya
Analytics Vidhya
Published in
4 min readDec 31, 2019
Photo by Safar Safarov on Unsplash

One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.

The Dart language supports the following types−

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Maps

Numbers:

int : Integer is a type of non fractional number value without. Here non fractional means a value without point. Integer dose not support POINT values. It can only hold pure numeric values. See the example below.

int tempNum = 123456;

print(tempNum);

// Output should be : 123456

double : Double are basically bigger type of FLOAT values. It can hold fractional decimal values. In dart the double support 64 bit double prescription values. double also represents floating point literals.

double i = 52.11 ;

print(i);

// Output should be : 52.11

String :

String data type represents a sequence of multiple characters text also known as group of multiple characters. In Dart string is sequence of UTF-16 code units. String can be created using both single quotes and double quotes but both should be same a creation time.

String name1 = ‘Flutter’;

String name2 = “Examples”;

print(name1 +’ ’+name2);

// Output should be : Flutter Examples

Boolean :

Boolean data type is used to hold true and false values. Boolean data type uses the ‘bool‘ keyword on declaration time.

bool val1 = true;

bool val2 = false;

print(val1);

print (val2);

// Output should be : true & false

Lists :

A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.

Each element in the List is identified by a unique number called the index. The index starts from zero and extends up to n-1 where n is the total number of elements in the List. The index is also referred to as the subscript.

  • Fixed Length List
var list_name = new List(initial_size)--------------------------------------------------------------------
void main() {
var lst = new List(3);
lst[0] = 12;
lst[1] = 13;
lst[2] = 11;
print(lst);
}
Output:
[12, 13, 11]
  • Growable List
var list_name = [val1,val2,val3]   
--- creates a list containing the specified values
OR
var list_name = new List()
--- creates a list of size zero
--------------------------------------------------------------------void main() {
var num_list = [1,2,3];
print(num_list);
}
Output:
[1, 2, 3]

List Properties

The following table lists some commonly used properties of the List class in the dart:core library.

first : Returns the first element case.

isEmpty : Returns true if the collection has no elements.

isNotEmpty : Returns true if the collection has at least one element.

length : Returns the size of the list.

last : Returns the last element in the list.

reversed : Returns an iterable object containing the lists values in the reverse order.

Single : Checks if the list has only one element and returns it.

Maps :

Same as List data type Map data type is also a type of group of multiple values. In Map the data stored in key:value pairs format. There is no bound for key value they can be in any type. Map date can also have null type of values. Map are like objects with multiple values.

void main() { 
var details = {'Usrname':'tom','Password':'pass@123'};
print(details);
}
Output :
{Usrname: tom, Password: pass@123}
--------------------------------------------------------------------void main() {
var details = {'Usrname':'tom','Password':'pass@123'};
details['Uid'] = 'U1oo1';
print(details);
}
Output :
{Usrname: tom, Password: pass@123, Uid: U1oo1}

Map — Properties

Keys : Returns an iterable object representing keys

Values : Returns an iterable object representing values

Length : Returns the size of the Map

isEmpty : Returns true if the Map is an empty Map

isNotEmpty : Returns true if the Map is an empty Map

Map — Functions

addAll() : Adds all key-value pairs of other to this map.

clear() : Removes all pairs from the map.

remove() : Removes key and its associated value, if present, from the map.

forEach() : Applies f to each key-value pair of the map.

How Can You Contribute?

Posts In This Series

Show Your Support

Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!

--

--