# Enum

## **How Enums Can Simplify Code**

```dart
enum PlanetType { terrestrial, gas, ice }
```

This is a basic **enum** that everyone knows. But what if your **enum** could:

* **Define itself in JSON** when received from a server
* **Automatically translate** into different languages
* **List all its values** dynamically
* **Search for values** within its own methods
* **Shorten conditional statements** significantly

In this article, I’ll show you **a different way to use enums**, turning a simple definition into **a powerful and efficient coding tool**.

Flutter is an **object-oriented** framework, and **encapsulation** is a key principle in OOP. Encapsulation hides the implementation details of an object and only allows interaction through **public methods**. This helps protect internal data, reduce errors, and improve maintainability.

Therefore, we can **incorporate all these useful functionalities** into a single **enum**, making it easier to use without worrying about its internal logic.

## **Defining Custom Fields in JSON**

Defining a custom field in JSON is introduced in the official documentation of **json\_serializable**:\
🔗 [json\_serializable - Enums](https://pub.dev/packages/json_serializable#enums)

Below is an example of how to set up an **enum** for `json_serializable`. The **enum** retrieves data from JSON based on the `'code'` field and converts it into an **enum value**.

For example, with the following JSON:

```json
{ "code" : 1 }
```

The corresponding **enum value** will be:

```dart
StateEnum.loading
```

We can replace `'code'` with any other field, such as `'title'`, depending on the requirements.

**Example Code:**

```dart
@JsonEnum(valueField: 'code')
enum StateEnum {
  notWorkingReady(0, "notWorkingReady"),
  loading(1, "loading"),
  notWorkingSuccess(3, "notWorkingSuccess"),
  notWorkingFailure(2, "notWorkingFailure");

  const StateEnum(this.code, this.title);

  final int code;
  final String title;
}
```

## **Shortening Conditional Code**

The second benefit that enums provide is reducing conditional code, making it easier to understand. Let's take a look at the following example:

```dart
@JsonEnum(valueField: 'code')
enum StateEnum {

    notWorkingReady( 0, "notWorkingReady"),
    loading( 1, "loading"),
    notWorkingSuccess( 3, "notWorkingSuccess"),
    notWorkingFailure( 2, "notWorkingFailure");
    
    // @override
    // int compareTo(PaddingWhiteLineEditEnum other) => code - other.code;
    
    const StateEnum( this.code, this.title);
    
    final int code;
    final String title;

    bool get isLoading => this == StateEnum.loading;

}
```

Writing concise conditions makes the code more readable and easier to understand, as shown below:

**New approach:**

```dart
if (state.isLoading) {
  
}
```

**Old approach:**

```dart
if (state == StateEnum.loading) {
  
}
```

## Language Translation

```dart
String get interpret => switch (this) {
      StateEnum.notWorkingReady => "Translate notWorkingReady",
      StateEnum.loading => "Translate loading",
      _ => "Translate other",
  };
```

I will write a separate article on. multi-language translation. You can check it out here

{% content-ref url="/pages/4HPxogOT3HRVQ8Hwj6mG" %}
[Multi-Language](/flutter/ui/multi-language.md)
{% endcontent-ref %}

## Retrieve Data from Enum Fields

Suppose I have the `code` field value and I want to find out which enum it corresponds to. I can create a function like this:

```dart
static ({StateEnum? getEnum, String? string})? getByCode(int? code) {
    try {
        final e = StateEnum.values.firstWhere((element) => element.code == code);
        return (getEnum: e, string: e.interpret);
    } catch (e) {
        return null;
    }
}
```

## Using Mason to Speed Up Enum Creation

With all the benefits listed above, using full-featured enums helps us write cleaner and faster code.

To generate a predefined template for your enums, consider using Mason. (If you're unfamiliar with Mason, check out this guide: [Boost Your Flutter Development Efficiency with Mason](https://mymoneycoupon.web.app/en/doc/help/flutter/boost-your-flutter-development-efficiency-with-mason))

{% content-ref url="/pages/6Yh2LXIXAdzRX2g1isw5" %}
[Mason](/flutter/easy-code/mason.md)
{% endcontent-ref %}

You can also check out my enum template and create a custom one for yourself and your colleagues:\
[dr\_enum on BrickHub](https://brickhub.dev/bricks/dr_enum)

[Buy Me a Coffee](https://buymeacoffee.com/ducmng12g) | [Support Me on Ko-fi](https://ko-fi.com/I2I81AEJG8)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wong-coupon.gitbook.io/flutter/my-dart/enum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
