Data Types in Dart
Data Types
Data types help you to categorize all the different types of data you use in your code. For e.g. numbers, texts, symbols, etc. The data type specifies what type of value will be stored by the variable. Each variable has its data type. Dart supports the following built-in data types :
- Numbers
- Strings
- Booleans
- Lists
- Maps
- Sets
- Runes
- Null
Built-In Types
In Dart language, there is the type of values that can be represented and manipulated. The data type classification is as given below:
Data Type | Keyword | Description |
---|---|---|
Numbers | int, double, num | It represents numeric values |
Strings | String | It represents 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 |
Sets | Set | It is an unordered list of distinct values of same types |
Runes | runes | It represents Unicode values of String |
Null | null | It represents null value |
Numbers
When you need to store numeric value on dart, you can use either int or double. Both int and double are subtype of num
. You can use num to store both int or double value.
void main() {
// Declaring Variables
int num1 = 100; // without decimal point.
double num2 = 130.2; // with decimal point.
num num3 = 50;
num num4 = 50.4;
// For Sum
num sum = num1 + num2 + num3 + num4;
// Printing Info
print("Num 1 is $num1");
print("Num 2 is $num2");
print("Num 3 is $num3");
print("Num 4 is $num4");
print("Sum is $sum");
}
Round Double Value To 2 Decimal Places
The .toStringAsFixed(2)
is used to round the double value upto 2 decimal places in dart. You can round to any decimal places by entering number like 2, 3, 4.
void main() {
// Declaring Variables
double price = 1130.2232323233233; // valid.
print(price.toStringAsFixed(2));
}
String
String helps you to store text data. You can store values like I love dart
, New York 2140
in String. You can use quotes to store string in dart.
void main() {
// Declaring Values
String schoolName = "Diamond School";
String address = "New York 2140";
// Printing Values
print("School name is $schoolName and address is $address");
}
Create A Multi-Line String In Dart
If you want to create a multi-line String in dart, then you can use triple quote with either single or double quotation marks.
void main() {
// Multi Line Using Single Quotes
String multiLineText = '''
This is Multi Line Text
with 3 single quote
I am also writing here.
''';
// Multi Line Using Double Quotes
String otherMultiLineText = """
This is Multi Line Text
with 3 double quote
I am also writing here.
""";
// Printing Information
print("Multiline text is $multiLineText");
print("Other multiline text is $otherMultiLineText");
}
Special Character In String
Special Character | Work |
---|---|
\n | New Line |
\t | Tab |
void main() {
// Using \n and \t
print("I am from \nUS.");
print("I am from \tUS.");
}
Create A Raw String In Dart
You can also create raw string in dart. Special characters won’t work here. You must write r
after equal sign.
void main() {
// Set price value
num price = 10;
String withoutRawString = "The value of price is \t $price"; // regular String
String withRawString =r"The value of price is \t $price"; // raw String
print("Without Raw: $withoutRawString"); // regular result
print("With Raw: $withRawString"); // with raw result
}
Type Conversion In Dart
In dart, type conversion allows you to convert one data type to other type. For e.g. to convert String to int, int to String or String to bool, etc.
Convert String To Int In dart
You can convert String to int using int.parse() method. The method takes String as an argument and converts it into an integer.
void main() {
String strvalue = "1";
print("Type of strvalue is ${strvalue.runtimeType}");
int intvalue = int.parse(strvalue);
print("Value of intvalue is $intvalue");
// this will print data type
print("Type of intvalue is ${intvalue.runtimeType}");
}
Convert Int To String In Dart
You can convert int to String using toString() method. Here is example:
void main() {
int one = 1;
print("Type of one is ${one.runtimeType}");
String oneInString = one.toString();
print("Value of oneInString is $oneInString");
// this will print data type
print("Type of oneInString is ${oneInString.runtimeType}");
}
Convert Double To Int In Dart
You can convert double to int using toInt() method.
void main() {
double num1 = 10.01;
int num2 = num1.toInt(); // converting double to int
print("The value of num1 is $num1. Its type is ${num1.runtimeType}");
print("The value of num2 is $num2. Its type is ${num2.runtimeType}");
}
Booleans
In Dart, boolean holds either true or false value. You can write bool
keyword to define boolean data type.
void main() {
bool isMarried = true;
print("Married Status: $isMarried");
}
Lists
The list holds multiple values in a single variable. It is also called arrays. If you want to store multiple values without creating multiple variables, you can use a list.
void main() {
List<String> names = ["Raj", "John", "Max"];
print("Value of names is $names");
print("Value of names[0] is ${names[0]}"); // index 0
print("Value of names[1] is ${names[1]}"); // index 1
print("Value of names[2] is ${names[2]}"); // index 2
// Finding Length of List
int length = names.length;
print("The Length of names is $length");
}
Note: List index always starts with 0. Here names[0] is Raj, names[1] is John and names[2] is Max.
Sets
An unordered collection of unique items is called set in dart. You can store unique data in sets.
Note: Set doesn’t print duplicate items.
void main() {
Set<String> weekday = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
print(weekday);
}
Maps
In dart, a map is an object where you can store data in key-value pairs. Each key occurs only once, but you can use same value multiple times.
void main() {
Map<String, String> myDetails = {
'name': 'John Doe',
'address': 'USA',
'fathername': 'Soe Doe'
};
print(myDetails['name']);
}
Var Keyword In Dart
In dart, var
automatically finds a data type. In simple terms, var says if you don’t want to specify a data type, I will find a data type for you.
void main(){
var name = "John Doe"; // String
var age = 20; // int
print(name);
print(age);
}
How To Check Runtime Type
You can check runtime type in dart with .runtimeType
after variable name.
void main() {
var a = 10;
print(a.runtimeType);
print(a is int); // true
}
Optionally Typed Language
You may hear of the Dynamically-typed
language. It means something can change, once you tell its type. Similarly, statically-typed
language means something cannot change, once you tell what type of variable it is. Dart support both dynamic type as well as static type, so it is also called optionally-typed language.
Statically Typed Example
void main() {
var myVariable = 50; // You can also use int instead of var
myVariable = "Hello";
print(myVariable);
}
Dynamically Typed Example
void main() {
dynamic myVariable = 50;
myVariable = "Hello";
print(myVariable);
}
Note: Using static type helps you to prevent writing silly mistakes in code. It’s a good habit to use static type in dart.