Add Your Heading Text Here

Data type in any language is a classification of data which tells the compiler or interpreter how the programmer wish to use data.

Data types are used generally in type safe languages (Type safety is the concept of preventing type errors while accessing different kinds of data types based on authority of access).

Dart 1.X used optionally type checking while Dart 2.X uses a combination of static type checking and runtime checks to ensure that a variable’s value always matches the variable’s static type.

The Dart supports following built in data types:

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Maps

Numbers

Dart number has 2 subtypes,

  1. Integer
  2. Double

int

Integer data type can have any value from natural numbers and their negative counterparts(i.e., …-3,-2,-1,0,1,2,3,…)

int keyword is used to declare integer data type variable.

For ex :

int a = 7;

double

Integer data type cannot accept decimal values so, double data type is Introduced with 64 bit range which accepts all the decimal values from range …-3,-2,-1,0,1,2,3,…

double keyword is used to declare double data type variable.

For ex:

double a = 7.7;

Strings

Strings are nothing but sequence of characters or bunch of characters. It can be used to store any number of characters like, for storing name, personal details, different kind of information etc.

String keyword is used to declare string data type variable.

For ex :

String a = “Ram”;

Boolean

Boolean data type is used whenever we want to apply conditional test on our logic. It can have only two values True or false.

bool keyword is used to declare boolean data type variable.

For ex :

bool x = 2>3; // so, x has value false.

Lists

list is nothing but the collection of different elements of the same type or collection of ordered group of objects.

So, list is used to store elements of particular kind of data type or to store objects.

In dart arrays are not used. instead dart uses arrays as list-objects.

For ex :

void main(){
	List<int> a=new List();  // int is  type of data
	a=[1,2,3,4,5];
	print(a);
} 

Output :

[1,2,3,4,5]

Maps

Maps is one of the best data Structure for storing the data in key-value pairs.

Each key is associated with one value in Maps. Both keys and values can be of any type for mapping.

For ex :

void main(){
	Map<String,String> a=new Map();
  	a={'Username':'BillGates','Password':'123456'};
 	print(a);}

Output :

{Username: BillGates, Password: 123456}

Dynamic Keyword

As we all know that dart is optionally typed language which means statically typed as well as dynamically typed language.

Statically typed language is one in which the data type of the variables or parameters are defined at the time of compilation or say declaration.

For ex :

String, Boolean, Map etc.

While Dynamically typed language is one in which the data type of the variables or parameters are defined at runtime. So, dart has dynamic keyword for declaring variable data type at runtime.

For ex :

void main()
{
    dynamic x=5;            // x will be evaluated at runtime as integer variable having value 5.
    dynamic y="Toastguyz";  // y will be evaluated at runtime as String variable having value "Toastguyz".
    dynamic z=true;         // z will be evaluated at runtime as boolean variable having value true.
    print(x);
    print(y);
    print(z);
}

Output :

5
Toastguyz
true

In this way, If you’re are not sure about the type of variable you can declare it as dynamic which will be explicitly evaluated based on value assigned to it.

So, these are the basics of for each data types. You must deep dive in to dart data types to quickly grasp the dart language.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons