Dart Programming

Dart Enum

Enums are a crucial component of computer languages. They assist developers in defining a small collection of predetermined values that will be used throughout the logic they create. Enums are limited in functionality in the Dart programming language, which is used to construct for Flutter. This article explains how to utilize enumerations in Dart (also referred to as enums or enumerated types).

What is Enum in Dart in Ubuntu 20.04?

Enumerated types, sometimes called enumerations or enum, specify named constant sets of values which can be members, elements, etc. When working with a small set of variables, this is critical. An enumeration type is defined in Dart using the enum keyword. Enumeration is a method for storing finite data members with the same type specification. The elements of an enumeration can be compared based on their identities, and the enumeration can be iterated over.

Syntax of Enum in Dart in Ubuntu 20.04

The enumeration is specified with the enum keyword, then a comma-separated list of valid identifiers. Within the curly brackets, this list is contained. The syntax representation of enum is as follows.

enum e_name {

ele1,

ele2,

.......

elen

}

The e_name is used to name the enumeration class, as the name implies. Prevent using a comma or semicolon when presenting the final data element.

How to Use the Enum in Dart in Ubuntu 20.04?

The role of enum in the dart programming language is explained in the following dart programs examples:

Example # 1: Program to Print Enum Element in a Dart in Ubuntu 20.04

This one is the first enum example where we are printing the enum elements along with their index position in the enum class type. In the enumeration list, each identifier has an index position.

Let’s begin with the program implementation. We have a keyword “enum” in the first step, which represents the enumeration class data type. The enum is assigned a name “Flowers,” and inside the enum type parentheses, we have initialized four elements which are the name of flowers. After the enum declaration, we have the program’s main function definition. Within the function main, we have initially printed the enum “Flowers” elements. Then used a for each method which has created a new variable, “e,” which has taken the values from enum “flowers”.

As you can see, we have also defined the index values of the enum elements inside the print statement through string interpolation expressions. We have displayed a separate value of the enum element “Lilly” with its index position in the enum class.

.

The overhead program produces the following results as an output.

Example # 2: Program to Print Enum Element Through Iteration in a Dart in Ubuntu 20.04

Here, we are printing all the enum elements through the iteration method. Let’s analyze the iteration cycle over the enum elements in below dart program.

Firstly, we have an enum class data type declaration. The enum class type is given the name “myList”. The enum class “myList” contains some elements which will be printed through the iteration method. We have used for loop here for printing the elements stored in the enum class. You can take any iteration method with your ease. Inside the for loop, the variable defines a new variable, “values” to which all the enum members have been assigned. Now, the “in” keyword inside the for loop will iterate over each element in the enum type and print the elements stored in a variable “value”.

Each element of the enum type is shown on the screen with the same sequence as we have initialized it.

Example # 3: Program of an Enum Using Switch Case in a Dart in Ubuntu 20.04

The switch block can be used for an enum type, and it takes case blocks for all instances of our enum class and a default block if a case-block implementation is absent; otherwise, a compilation error will occur.

Note that constants and enums can both have switch block implementations. When you don’t want to miss out on a chance to address a specific instance, though, enums are better.

In the following dart program, we have defined an enum class with the name “planets,” Within the enum class type, we have a set of elements that are members of the enum class. After that, the main function is used where the switch case is implemented. Firstly, we have constructed a variable with the “var” keyword, and the variable represents the name “your planet”. Suppose that we are taking the value from the user with the help of declaring a variable and assigning it an element of the enum class. Then, we have a switch statement where the variable “yourPlanet” is passed as an argument. The switch statement will switch the case until the given case is not reached.

Consider that the enumerated class does not save all forms of data; instead, it only stores string entries without the quotes. The output from the above program is displayed over the screen.

Example # 4: Program of an Enum Using Enum Elements in a Class in a Dart in Ubuntu 20.04

Considering the case below, which uses enum members as class objects. In the following example, we have constructed the enum class as “Dept” and created its member inside it. After that, we defined a class as “employee”, and inside the class, we created the class objects with the “final” keyword. This final keyword is used to prohibit derived classes from overriding it. Then outside the class, the class instance “employee1” is deployed, which values the class members. Within the main function, we have the if condition, which will display the statement of the matched condition; otherwise, the statement will be printed.

The output shows the results of the above program.

Conclusion

We have covered the basics of enumerations in the dart programming language. We looked at various approaches to enumeration and how to initialize it in the dart with the example illustration. Each technique has its own benefits, and several approaches can be used based on the use case. But we have some limitations of enum data type. It can’t be subclassed or mixed in with anything else. Also, explicitly instantiating an enum is not possible.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.