Same as other programming languages Dart has also Data Types. Data types defines which type of data a variable can store. Dart comes with 6 types of Data types and within it the number type has 4 sub data types. We will be discussing all of them with example.
Data Types in Dart - Flutter
- Number - Int, Double, Number & BigInt
- String
- Boolean
- List
- Map
- Runes
1. Int : Also known as Integer. Integer variables stores value without point. If we pass point value then it will stores only before point number value. If we assign point value double to int then it will throw us an error "A value of type 'double' can't be assigned to a variable of type 'int' ".
void main() {
int a = 101;
print(a);
}
Output:
2. Double : Double data type is almost same as integer. The main difference between them is that double can store point value. In programming language it can store 64 bit floating numbers.
void main() {
double a = 105.2253;
print(a);
}
Output:
3. Num : Number data type is the upgraded version of int and double. Num alone can store both integer and double values.
void main() {
num a = 101.22520;
num b = 101;
print(a);
print(b);
}
Output:
void main() {
BigInt bi1 = BigInt.parse("232323232323232323232");
print(bi1);
}
Output:
5. String : String variables in dart can store both Char and string values. String value are defined as group of characters.
void main() {
String a = "hello";
String b = 'Friends';
print(a+ ' ' +b);
}
Output:
6. Boolean : Boolean variables stores only 2 values True and False. Here True means 1 and False means Zero.
void main() {
bool a = true;
bool b = false;
print(a);
print(b);
}
Output:
7. List : List in dart is same as Array in JavaScript. List is a putting single type of different data managing with index number in single variable. We cannot create empty list in Dart.
void main() {
// Create List with 7 Fixed Items and fill 0 in Each Item.
final fixedLengthList = List < int >.filled(7, 0);
print(fixedLengthList);
// Updating value of 0 Index.
fixedLengthList[0] = 1 ;
// Updating value of 1 Index.
fixedLengthList[1] = 2 ;
// Updating value of 2 Index.
fixedLengthList[2] = 3 ;
// Updating value of 3 Index.
fixedLengthList[3] = 4 ;
// Updating value of 4 Index.
fixedLengthList[4] = 5 ;
// Updating value of 5 Index.
fixedLengthList[5] = 6 ;
// Updating value of 6 Index.
fixedLengthList[6] = 7 ;
print(fixedLengthList);
}
Output:
8. Map : Map is combination of KEY-VALUE pair in dart. In map we can store multiple type of data in form of object.
void main() {
// Map with Fixed type.
Map<String, String> tempMapFixed = {
'first_name': 'Sam',
'last_name': 'smith',
'number': '1234567890'};
print(tempMapFixed);
// Map with All type.
Map tempMapAll = {
"name" : "Sam",
"phoneNumber" : 1234569785,
"id" : 0123,
};
print(tempMapAll);
// Direct Map Define.
var mapTemp = {
"name" : "Sam",
"phoneNumber" : 1234569785,
"id" : 0123,
};
print(mapTemp);
}
Output:
void main() {
var smile = '\u263A';
print(smile);
}
Output:
Comments
Post a Comment