Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Enhanced Enums with Members In Dart

Like many programming languages, Dart permits you to make enums for characterizing limited sets of values. Here and there, you might need to add fields or works to your enum. For instance, there is an enum comprising conceivable situations with every status having a unique code. That was unimaginable until Dart 2.17, which presented improved enums with members.

This article will explore the Enhanced Enums with Members In Dart. We will perceive how to execute a demo program and we are going to learn about how we can use it in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

Creating Enhanced Enum with Members

Finding Enum by Field Value

Code FIle

Conclusion



Creating Enhanced Enum with Members:

The following is a fundamental enum with practically no members.

 enum Status {
pending,
success,
failed,
;
}

We will add a field code to the enum. To do as such, first, we need to add the field to the enum (very much like adding a field to a class in Dart). We likewise need to make a constructor with the field as the boundary and each characterized enum esteem value be characterized by calling the constructor.

  enum Status {
pending(1),
success(2),
failed(3),
;

final int code;

const Status(this.code);
}

In the model above, the code field is final, and that implies it must be instated. It doesn’t permit null values also. If you have any desire to permit null values, simply make the field nullable by adding ?after the sort (for example int?).

You can have more than one field on your enum. Furthermore, you can likewise make your capabilities, similar to the model beneath.

enum Status {
pending(1, false),
success(2, true),
failed(3, true),
;

final int code;
final bool isFinal;

const Status(this.code, this.isFinal);

String getUpperCaseName() {
return name.toUpperCase();
}
}

Finding Enum by Field Value:

Now and again, we need to get the enum value based on its field value. For instance, we need to get the Status enum above by utilizing the given code value.

For that reason, simply make a function that acknowledges the field value as the boundary. Inside the capability, repeat over the possible values which can be acquired from the values field.

 enum Status {
//...
static Status? getByCode(int code) {
for (Status status in Status.values) {
if (status.code == code) {
return status;
}
}

return null;
}
//...
}

Be that as it may, the above code isn’t productive since it will repeat over the values each time the capability is called. The arrangement is by caching the values to a Map.

enum Status {
//...
static final Map<int, Status> byCode = {};

static Status? getByCode(int code) {
if (byCode.isEmpty) {
for (Status status in Status.values) {
byCode[status.code] = status;
}
}

return byCode[code];
}
//...
}

Create a new dart file called main.dart inside the lib folder.

import 'package:enums_demo/enums_demo.dart';

void main() {
print(Status.success.getUpperCaseName());
print(Status.success.toString());
print(Status.getByCode(1));
}

When we run the application, we ought to get the screen’s output like the underneath screen Console Output.

SUCCESS
success(2)
pending(1)

Process finished with exit code 0

Code File:

enums_demo.dart

enum Status {
pending(1, false),
success(2, true),
failed(3, true),
;

final int code;
final bool isFinal;

static final Map<int, Status> byCode = {};

const Status(this.code, this.isFinal);

static Status? getByCode(int code) {
if (byCode.isEmpty) {
for (Status status in Status.values) {
byCode[status.code] = status;
}
}

return byCode[code];
}

String getUpperCaseName() {
return name.toUpperCase();
}

@override
String toString() {
return "$name($code)";
}
}

main.dart

import 'package:enums_demo/enums_demo.dart';

void main() {
print(Status.success.getUpperCaseName());
print(Status.success.toString());
print(Status.getByCode(1));
}

Conclusion:

In the article, I have explained the Enhanced Enums with Members structure in a dart; you can modify this code according to your choice. This was a small introduction to Enhanced Enums with Members User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Enhanced Enums with Members of your projects. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.