Dart Collections

Learn via video courses
Topics Covered

Overview

Building complex applications is not just about making them visually appealing but we also need to handle and manipulate data. Apart from providing multiple tools for UI development, Flutter is also known to provide the utilities to proficiently manage data. It provides us with multiple data structures which help us in data management. Diverse ranges of classes are available in Flutter and each class has unique properties which help in data collection in a certain manner. These classes are Enums, Lists, Queues, Sets, and Maps etc. We will discuss the above classes in the course of this article.

Introduction

Flutter provides us with multiple data collection classes which help the developers in data collection. These classes help us not only in enhancing the user experience but also in improving the overall performance of the application. Multiple data collection classes help in a variety of scenarios. There is a symbiotic relationship between the design and data components of our Flutter applications. Flutter provides multiple data collection tools such as Lists, Enums, Queues, Sets, and Maps. Each of these data structures is unique and serves distinct purposes in data management. Lists allow us the ordered storage of elements, Enums offer a simple way of defining a set of distinct values, Queues are based on first-in, first-out (FIFO), Sets do not allow the items to get repeated and Maps allow us to pair the values with a corresponding key. We will look at all of these data structures one by one.

Enum

Enum, short for enumeration is a way of data collection in Flutter that comprises a set of named constant values. They prove to be immensely helpful when we have to represent multiple options or states within an application.

Here is a simple example of how we can define an Enum:

In this case, we've defined an Enum called Student with four possible values: Topper, Studious, Average, and Backbencher.

Enums find applications in various scenarios within Flutter projects. For instance, consider a simple app that discusses the type of Student. Instead of using numerical codes to represent the Student, Enums offer descriptive values which make the code quality much better

Example with complete code

In another example, we will look at how we can use enums to design a simple functionality that allows the user to choose from three of the most popular vegetarian North Indian Cuisine, i.e., Kadhai Paneer, Shahi Paneer, and Matar Paneer.

Output:

Some of the benefits of using enums include:

1. Readability: Enums make our code more readable as they replace arbitrary numbers with meaningful words making it easier for us to read and comprehend.

2. Safety: Enums are a way of data collection which provides type safety, reducing the likelihood of errors that could occur when using raw values.

3. Documentation: Enums inherently document the possible values, making it easier for developers to understand the options available.

4. Code Maintenance: If new values need to be added, Enums make it clear where and how these changes should be made.

List

A List in Flutter is an ordered collection of elements, each identified by an index. Lists are dynamic in nature, meaning they can grow or shrink as needed.

Lists help us in a variety of use cases in App Development as they facilitate us to store data sequentially. Anything ranging from text messages, blog posts, images, videos, or music can be stored and accessed using a list. Other than this, Lists also provide some methods such as add(), remove(), insert(), and sort() which make it very convenient for the developers to handle data using these.

Some of the benefits of using Lists include:

1. Order Preservation: Lists maintain the order in which elements are added, ensuring predictable sequencing.

2. Dynamic Resizing: Lists can grow or shrink, accommodating changing data requirements.

3. Efficient Access: Lists are a form of data collection that allows us to access the elements by index in Lists very efficiently.

4. Versatility: Lists serve as a foundation for more complex data structures and algorithms.

Example with complete code

Output:

Queue

The Queue is one of the most commonly used data structures for data collection across multiple programming languages and frameworks. It is based on the First-In, First-Out, or (FIFO). Queues find their utility in scenarios where tasks must be executed in a specific order.

Some of the methods provided by the queue data structure are: add(), remove(), and isEmpty().

Queues prove valuable in scenarios that require tasks to be processed in a specific order:

1. Background Jobs: In applications that require asynchronous processing, Queue data collections can ensure that background tasks are executed in the order they're received.

2. Task Scheduling: Task schedulers often utilize Queues to manage the execution sequence of scheduled tasks.

3. Event Handling: Queues can be used to manage events in applications where event order is crucial, such as in games.

Example with complete code

Output:

Set

A Set in Flutter is an unordered collection of unique elements. Unlike Lists, Sets do not allow duplicate values. We use sets in those cases where the duplicity of elements or data items is not allowed and all the items must be present exactly once. One of the major pros of using the set data structure for data collection is that it allows us to access the data fast.

Some of the methods provided by the queue data structure are: add(), remove(), and contains().

Sets are particularly used in cases where each data item must be stored exactly once. They are also used in categorization as each data item must go to a unique category. Additionally, sets can be used to implement data deduplication by removing duplicate entries

Some of the benefits of using Sets are:

1. Data Integrity: Sets prevent the inclusion of duplicate values, maintaining the accuracy and reliability of your data.

2. Efficient Search: Set data collection supports operations such as adding and searching are optimized for fast performance.

3. Distinct Values: Sets inherently guarantee that all elements are unique within the collection.

Example with complete code

Output:

Map

A Map in Flutter is a collection of key-value pairs, where each key is unique and associated with a corresponding value. Map is a very important way of data collection in Flutter as it allows us to get the value corresponding to an entry extremely efficiently.

Some of the methods provided by the queue data structure are: put(), remove(), and containsKey().

Maps are commonly used to store user information in the form of key-value pairs for attributes like names and emails etc. They are also used for language translation where the respective translated string is placed corresponding to a particular key.

Some of the major benefits of using Maps is as follows:

1. Customized Data Structures: Maps allow you to create flexible and custom data structures tailored to your application's needs.

2. Relationship Modeling: Maps excel at representing complex relationships between different pieces of data.

3. Efficient Data Retrieval: Retrieving values from a Map using keys is optimized for fast performance.

Example with complete code

Output:

Conclusion

  1. Flutter offers a range of data collection classes - Enums, Lists, Queues, Sets, and Maps - each serving distinct purposes.

  2. Each of the data structures has its unique properties for which they are used.

  3. Enums, Lists, Queues, Sets, and Maps find applications in various scenarios from UI components to background task processing.

  4. Sets and Maps particularly enforce data integrity by preventing duplicates and managing associations.

  5. These data collection classes enhance code efficiency, readability, and maintenance.

  6. By mastering these classes, developers can create apps that handle data with precision, organization, and user-centricity.