Flutter notes
Datatypes in Dart (Flutter) – Detailed Explanation
In Dart, datatypes define the type of values a variable can store. Dart is a statically
typed language, meaning each variable must have a type. Dart also supports type inference,
meaning it can automatically detect the type when a variable is assigned a value.
1. Number Data Types
Numbers in Dart can be classified into three types:
Datatype Description
int Represents whole numbers (positive, negative, zero) without decimals.
double Represents floating-point numbers (decimal values).
num A supertype of both int and double, allowing a variable to hold either type.
1.1. int (Integer)
Used to store whole numbers (without decimal points).
Example:
dart
Copy code
void main() {
int age = 26;
int population = 1000000;
int negativeValue = -42;
print(age); // Output: 26
print(population); // Output: 1000000
print(negativeValue); // Output: -42
}
1.2. double (Floating-Point Number)
Stores decimal numbers (floating-point values).
Example:
dart
Copy code
void main() {
double price = 199.99;
double pi = 3.14159;
print(price); // Output: 199.99
print(pi); // Output: 3.14159
}
1.3. num (Supertype of int and double)
Allows a variable to store both int and double.
Example:
dart
Copy code
void main() {
num number1 = 10; // int
num number2 = 10.5; // double
print(number1); // Output: 10
print(number2); // Output: 10.5
}
1.4. Number Operations
Dart supports standard arithmetic operations:
dart
Copy code
void main() {
int a = 10;
double b = 5.5;
num c = 20;
print(a + b); // Addition (15.5)
print(a - b); // Subtraction (4.5)
print(a * b); // Multiplication (55.0)
print(a / b); // Division (Result is double → 1.818...)
print(a ~/ b); // Integer Division (Result is int → 1)
print(a % b); // Modulus (Remainder → 4.5)
}
2. String Data Type (String)
A String is a sequence of characters, enclosed in single (' ') or double (" ") quotes.
2.1. Declaring Strings
dart
Copy code
void main() {
String name = "Flutter";
String language = 'Dart';
print(name); // Output: Flutter
print(language); // Output: Dart
}
2.2. String Interpolation ($ or ${})
Allows inserting variables inside a string using $ or ${}.
dart
Copy code
void main() {
String name = "Dart";
int version = 3;
print("Welcome to $name version $version");
// Output: Welcome to Dart version 3
print("Square of 4 is ${4 * 4}");
// Output: Square of 4 is 16
}
2.3. Multi-line Strings
dart
Copy code
void main() {
String multiLine = '''This is
a multi-line
string.''';
print(multiLine);
}
3. Boolean Data Type (bool)
Represents true or false values.
Used in conditions and logical operations.
Example
dart
Copy code
void main() {
bool isFlutterEasy = true;
bool isDartDifficult = false;
print(isFlutterEasy); // Output: true
print(isDartDifficult); // Output: false
}
4. Lists (List<T>) – Ordered Collection
A List in Dart is an ordered collection of elements, like an array.
4.1. Declaring Lists
dart
Copy code
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<String> fruits = ["Apple", "Banana", "Mango"];
print(numbers); // Output: [1, 2, 3, 4, 5]
print(fruits); // Output: [Apple, Banana, Mango]
}
4.2. Accessing List Elements
dart
Copy code
void main() {
List<String> colors = ["Red", "Blue", "Green"];
print(colors[0]); // Output: Red
print(colors.length); // Output: 3
}
4.3. Modifying a List
dart
Copy code
void main() {
List<int> numbers = [10, 20, 30];
numbers.add(40); // Adds an element
numbers.remove(20); // Removes an element
print(numbers); // Output: [10, 30, 40]
}
5. Sets (Set<T>) – Unique Collection
A Set is an unordered collection of unique elements (no duplicates).
5.1. Declaring Sets
dart
Copy code
void main() {
Set<int> uniqueNumbers = {1, 2, 3, 4, 5};
print(uniqueNumbers); // Output: {1, 2, 3, 4, 5}
}
5.2. Adding and Removing Elements
dart
Copy code
void main() {
Set<String> cities = {"New York", "London", "Paris"};
cities.add("Tokyo");
cities.remove("London");
print(cities); // Output: {New York, Paris, Tokyo}
}
6. Maps (Map<K, V>) – Key-Value Pair Collection
A Map is a collection of key-value pairs, like a dictionary in Python.
6.1. Declaring Maps
dart
Copy code
void main() {
Map<String, int> marks = {
"Math": 90,
"Science": 85,
"English": 88
};
print(marks); // Output: {Math: 90, Science: 85, English: 88}
}
6.2. Accessing and Modifying a Map
dart
Copy code
void main() {
Map<String, String> capital = {
"India": "New Delhi",
"USA": "Washington D.C."
};
print(capital["India"]); // Output: New Delhi
capital["UK"] = "London"; // Add new key-value pair
print(capital); // Output: {India: New Delhi, USA: Washington D.C., UK:
London}
}
7. Dynamic Type (dynamic)
Allows a variable to hold any type of value.
Type checking happens at runtime.
Example
dart
Copy code
void main() {
dynamic variable = "Hello";
print(variable); // Output: Hello
variable = 123;
print(variable); // Output: 123
}
Conclusion
Dart provides various datatypes to handle different kinds of data efficiently. Choosing the right
datatype improves performance and memory usage in Flutter applications.