What Data Types In dart Programming? Dart, a versatile and type-safe programming language developed by Google, provides different data types to handle different types of data.
Understanding these data types is critical to writing efficient and robust Dart code. This article looks at Dart’s basic data types, such as numbers, strings, booleans, and more complex types, such as lists, arrays, and maps..
Numbers
When you need to store numeric value on dart, you can use either int or double. Both int and double are subtypes of num. You can use num to store both int or double value.
Dart provides several numeric data types to represent numbers:
- int: Represents whole numbers, such as 1, 2, 3, etc.
- double: Represents 64-bit floating-point numbers, which can include decimal points.
- num: An inherited type of
intanddouble, allowing a variable to hold either type of number.
These types are essential for performing arithmetic operations, representing quantities, and handling numeric data in Dart applications
void main() {
// Declaring Variables
int num1 = 100;
double num2 = 130.2;
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");
}Strings
Strings in Dart are sequences of characters enclosed in single or double quotes. They are used to represent textual data. Strings in Dart are immutable, meaning once a string is created, it cannot be changed
void main() {
String FirstName = "John";
String LastName = "Doe";
// Printing Values
print("My Name is $FirstName and last Name is $LastName");
}Booleans
Booleans is represent two values: true and false. They are used to perform logical operations and control the flow of execution in a program. Dart’s type safety ensures that boolean values are used appropriately, preventing misuse of non-boolean values in conditions
bool isActive = true;
OR
bool isGreather = false Collections
CollectionsDart provides several types of collection for storing and managing groups of data:
- List: An ordered group of items. Lists can contain any type of objects and are editable.
- Set: An unordered collection of unique items. Arrays are useful when you want to make sure that each item appears only once.Map: a collection of key-value pairs.
- Maps: are useful for storing data that can be accessed by a unique key.These collections are important for organizing and manipulating data in Dart applications..
void main() {
var lst = new List(3);
lst[0] = 12;
lst[1] = 13;
lst[2] = 11;
print(lst);
}
OR
var list_name = [1,2,3];
OR
List<String> fruits = ["apple", "banana", "stawberry"];
print("Value of names[0] is ${fruits[0]}"); It will show the result output [12, 13, 11]
Map
Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime.
Declaring a Map using Map Literals
void main() {
var bio = {'firstName':'John','LastName':'Doe','email':'johndoe@gmail.com'};
print(bio);
}Declaring a Map using a Map Constructor
Final and Const
Final and Const use to declare a constant variable.
final result=10;
const pi = 3.14; Special Types
Dart also includes special types with specific roles in the language:
- dynamic: Indicates that a variable can hold any type of value. It disables static type checking.
- void: Indicates that a function does not return a value.
- Null: Represents the absence of a value. Dart’s null safety features help prevent null reference errors.