Logo

dev-resources.site

for different kinds of informations.

Dart enhanced enum with custom values

Published at
1/4/2023
Categories
dart
enum
Author
safalshrestha109
Categories
2 categories in total
dart
open
enum
open
Author
16 person written this
safalshrestha109
open
Dart enhanced enum with custom values

An enum type is a special data type that enables for a variable to be a set of predefined constants. It helps to make code readable and easier to understand.

For example, you might use an enum to define the gender of the people, like this:

enum Gender{
  Male,
  Female,
  Other
}
Enter fullscreen mode Exit fullscreen mode

Now if you run the following code

void main() {
  print(Gender.Female.index);          // 1
  print(Gender.Male.index);            // 0
  print(Gender.Male);                  //Gender.Male
  print(Gender.values[2]);             //Gender.Other
}
Enter fullscreen mode Exit fullscreen mode

We can access index *of the element defined and element using the *index.

Now what if we want to define custom value to the element of our enum. What if we want to assign number 1 to Male, 2 to Female and null to Other, and what if we want to return element of enum from the values that we defined for the element?

How can we do so ?

enum Gender {
  Male(1),
  Female(2),
  Other(null);
  const Gender(this.number);
  final int? number;
  static Gender getByValue(int? i) {
    return Gender.values.firstWhere((element) => element.number == i);
  }
}

void main() {
  print(Gender.getByValue(1));    //Gender.Male 
  print(Gender.getByValue(2));    //Gender.Female
  print(Gender.getByValue(3));    //Uncaught Error: Bad state: No element
  print(Gender.getByValue(null)); //Gender.Other
  print(Gender.Other.number);     //null
  print(Gender.Female.index);     //1
  print(Gender.values[2]);        //Gender.Other
}
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, we defined our values to the element of the the enum Gender. So, we can access the element of enum using the values we defined and vice versa. We can also access element using index and values like before. To use the values we defined, we can use the function getByValue that we created and number variable we defined.

Thanks for Reading! ✌️

enum Article's
30 articles in total
Favicon
Mastering ENUMs in Go
Favicon
The 3 kinds of Enum in Rails
Favicon
Enhancing Enum Constants in Laravel with Description and Label Attributes
Favicon
Should I stay or should I go? Enums in TypeScript - error case study
Favicon
Effective Enum
Favicon
Result in Rust: another way of error handling
Favicon
How to convert a TypeScript built-in enum to a GraphQL enum
Favicon
Leveraging Enums in Angular: Enhancing Code Clarity and Reliability
Favicon
Typescript enum vs. "as const"
Favicon
Enum Fábrica Solitão em Java
Favicon
Implementing Enums in Golang
Favicon
Elixirでリストの最初の要素を取得
Favicon
[JS TS] How to create an object with keys based on enum
Favicon
Enum ou Enumerations no PHP
Favicon
Replacing If-Else Statements with Enums in Java
Favicon
Dart enhanced enum with custom values
Favicon
Working with Enums in Rust
Favicon
Modelando algoritmos complexos com enum
Favicon
Managing Task Status Transitions in TypeScript with Enums and Object Mapping
Favicon
TypeScript Enum's vs Discriminated Unions
Favicon
Strategy Pattern no Spring Boot Usando Enum
Favicon
Problem in C Pointer
Favicon
Merge TypeScript Enums
Favicon
Agora sim, o grande ganho do enum no Dart 2.17
Favicon
How to proper use ambient enum from Definition file
Favicon
O Poder da Classe Enum com métodos abstratos
Favicon
Refactoring code with the Strate Pattern
Favicon
🔥Input Enums for a better Development Experience 🔥 😋✂️
Favicon
O pequeno grande ganho do enum no Dart 2.15
Favicon
Typescript enums drawbacks and solutions

Featured ones: