Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there is the type of values that can be represented and manipulated in a programming language. The data type classification is as given below:
|
Number | int, double, num, BigInt | Numbers in Dart are used to represent numeric literals |
Strings | String | Strings represent a sequence of characters |
Booleans | bool | It represents Boolean values true and false |
Lists | List | It is an ordered group of objects |
Maps | Map | It represents a set of values as key-value pairs |
1. Number: The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as:
- The int data type is used to represent whole numbers.
- The double data type is used to represent 64-bit floating-point numbers.
- The num type is an inherited data type of the int and double types.
Dart
void main() {
int num1 = 2;
double num2 = 1.5;
print(num1);
print(num2);
var a1 = num.parse( "1" );
var b1 = num.parse( "2.34" );
var c1 = a1+b1;
print( "Product = ${c1}" );
}
|
Output:
2
1.5
Product = 3.34
2. String: It used to represent a sequence of characters. It is a sequence of UTF-16 code units. The keyword string is used to represent string literals. String values are embedded in either single or double-quotes.
Dart
void main() {
String string = 'Geeks' 'for' 'Geeks' ;
String str = 'Coding is ' ;
String str1 = 'Fun' ;
print (string);
print (str + str1);
}
|
Output:
GeeksforGeeks
Coding is Fun
3. Boolean: It represents Boolean values true and false. The keyword bool is used to represent a Boolean literal in DART.
Dart
void main() {
String str = 'Coding is ' ;
String str1 = 'Fun' ;
bool val = (str==str1);
print (val);
}
|
Output:
false
4. List: List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects.
Dart
void main()
{
List gfg = new List(3);
gfg[0] = 'Geeks' ;
gfg[1] = 'For' ;
gfg[2] = 'Geeks' ;
print(gfg);
print(gfg[0]);
}
|
Output:
[Geeks, For, Geeks]
Geeks
5. Map: The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection.
Dart
void main() {
Map gfg = new Map();
gfg[ 'First' ] = 'Geeks' ;
gfg[ 'Second' ] = 'For' ;
gfg[ 'Third' ] = 'Geeks' ;
print(gfg);
}
|
Output:
{First: Geeks, Second: For, Third: Geeks}
Note: If the type of a variable is not specified, the variable’s type is dynamic. The dynamic keyword is used as a type annotation explicitly.