Hello! In this article, we will take a deep dive into a key component of Flutter, which is the collection types. Flutter is not only a powerful UI toolkit but is also based on the Dart language. In Dart, collection types play a very important role in managing data structures. They help efficiently process and manage various types of data.
Basic Concepts of Collection Types
Collection types are fundamentally ways to group and process data. Dart primarily offers three collection types: List, Set, and Map.
- List: A set of ordered data. It allows duplicate values and accesses data using an index.
- Set: A set of data that does not allow duplicates. It is very useful for adding, removing, and searching.
- Map: A set of data composed of key-value pairs. Each key is unique, and values are accessed through these keys.
1. List
A List is a collection type that allows storing multiple data while maintaining the order of data. A List can be declared as follows:
List numbers = [1, 2, 3, 4, 5];
In the example above, we created a List of type int
. Lists provide various methods to handle data. For example:
add(item)
: Adds a new item at the end of the list.remove(item)
: Removes a specific item from the list.contains(item)
: Checks if a specific item is included in the list.
To gain a deeper understanding of Lists, let’s look at the example below:
void main() {
List fruits = ["apple", "banana", "cherry"];
// Add item
fruits.add("orange");
// Remove item
fruits.remove("banana");
// Print list contents
print(fruits); // Output: [apple, cherry, orange]
// Check if specific item is included
if (fruits.contains("cherry")) {
print("Cherry is in the list.");
}
}
1.1 Iterating through a List
To access each item in a List, you can use a loop:
for (var fruit in fruits) {
print(fruit);
}
Alternatively, you can access items directly using an index:
for (int i = 0; i < fruits.length; i++) {
print(fruits[i]);
}
2. Set
A Set is a collection that does not allow duplicates. If you try to store duplicate values, they will be ignored. A Set can be declared as follows:
Set colors = {"red", "blue", "green"};
You can also use various methods in a Set:
add(item)
: Adds an item to the Set. If it is a duplicate, it will be ignored.remove(item)
: Removes a specific item from the Set.contains(item)
: Checks if a specific item is included in the Set.
Here is an example of using a Set:
void main() {
Set animals = {"cat", "dog", "bird"};
// Add item
animals.add("rabbit");
// Add duplicate item (ignored)
animals.add("cat");
// Print Set contents
print(animals); // Output: {cat, dog, bird, rabbit}
// Check if specific item is included
if (animals.contains("dog")) {
print("The dog is in the Set.");
}
}
3. Map
A Map is a collection that stores data in key-value pairs. The key acts as a unique identifier for each value. A Map can be declared as follows:
Map studentGrades = {"John": 85, "Kim": 90};
A Map also provides several methods to manage data:
put(key, value)
: Adds a new key-value pair to the Map.remove(key)
: Removes the item associated with a specific key.containsKey(key)
: Checks if a specific key is included in the Map.
3.1 Example of Using a Map
Here is a simple example of using a Map:
void main() {
Map studentGrades = {"John": 85, "Kim": 90};
// Add new student
studentGrades["Lee"] = 95;
// Print specific student's grades
print("Lee's grades: ${studentGrades["Lee"]}");
// Print all students and their grades
studentGrades.forEach((name, grade) {
print("$name: $grade");
});
}
Utilizing Collection Types
Let’s look at a practical example of utilizing collection types. This will help us understand how to use data structures in real development situations.
Example: Storing User Information
Assuming we are developing an application to store user information. In this case, we can utilize Lists and Maps:
class User {
String name;
int age;
User(this.name, this.age);
}
void main() {
List users = [];
// Add users
users.add(User("John", 25));
users.add(User("Kim", 30));
// Print user information
for (var user in users) {
print("Name: ${user.name}, Age: ${user.age}");
}
// Create Map with user names as keys
Map userMap = {for (var user in users) user.name: user};
// Search for a user
String searchName = "John";
if (userMap.containsKey(searchName)) {
User foundUser = userMap[searchName]!;
print("${foundUser.name}'s age is ${foundUser.age}.");
}
}
6. Conclusion
In this session, we took a close look at the collection types in Flutter, which can effectively manage data structures: List, Set, and Map. Each collection type has its distinct characteristics, and developers can choose the appropriate data depending on the situation. Through effective data management, developers can write more efficient and maintainable code.
Additionally, one should also consider potential performance issues or memory management that may arise when using collection types. Keep these points in mind as you design your own appropriate data structures. We will strive to cover in-depth topics in the next session as well. Thank you!