đ Dart Enums: The Hero We Deserve (But Donât Always Understand)
Ah, enums. Those unsung heroes of programming. Like the Avengers assembling in your codebase, they bring order to chaos by giving a name to your fixed sets of values. But, letâs be honest: when was the last time you truly appreciated an enum? (Yeah, thought so.) Letâs change that today by diving deep into the magical world of Dart enumsâcomplete with all the quirks, corner cases, and spicy code examples weâve discussed before.
Whatâs an Enum Anyway?
Enums (short for enumerations) are essentially the fancy VIP lists of programming. Instead of letting your code run wild with random strings like "male", "banana", or "yesPlease", enums step in and say:
âHey, I only allow these specific values. No riff-raff allowed.â
Example: The Basic Enum
enum Gender {
none,
male,
female,
others,
}
This lets you write beautiful, type-safe code like:
final myGender = Gender.male;
if (myGender == Gender.female) {
print("She's coding!");
}
Enums with Extra Spicy Properties
Sure, enums can just be a boring list of options, but in Dart, they can wear multiple hats! Letâs make them sassy with some custom properties:
Custom Properties in Enums
enum Gender {
none("none", "None"),
male("male", "Male"),
female("female", "Female"),
others("others", "Others");
final String dbValue;
final String faceValue;
const Gender(this.dbValue, this.faceValue);
}
Now you can access additional info:
print(Gender.male.dbValue); // Outputs: "male"
print(Gender.female.faceValue); // Outputs: "Female"
You can even pretend youâre fancy, like the Netflix of code enums:
âThis enum comes with bonus features.â
Finding Values in Enums
Remember when your code tried to match a string to an enum value but ended up crying in a corner? Fear not, for Dartâs firstWhere method is here to save the day.
Find by Property
Gender fromString(String value) {
return Gender.values.firstWhere(
(e) => e.dbValue == value,
orElse: () => Gender.none, // Fall back gracefully
);
}
final gender = fromString("female");
print(gender); // Outputs: Gender.female
Pro tip: Always use orElseâunless you enjoy mysterious crashes in production. ("How do you debug null? Asking for a friend.")
Excluding Values from Enums
Sometimes, you donât want all enum values. Maybe Gender.none isnât valid for a dating app (awkward silence). You can filter values like this:
Exclude a Value
final validGenders = Gender.values.where((g) => g != Gender.none).toList();
print(validGenders); // Outputs: [Gender.male, Gender.female, Gender.others]
When the Enum Plays Hard to Get
Letâs talk about a scenario where youâve created a factory method in your enum to convert strings into enum values. But oops! You try to return null, and Dart gives you the side-eye.
Returning null Gracefully
Gender? fromStringNullable(String value) {
return Gender.values.firstWhere(
(e) => e.dbValue == value,
orElse: () => null, // Dart gets salty if this is a non-nullable function
);
}
If your function expects a Gender but youâre trying to return null, Dart will fight you harder than a toddler over the last cookie. Always match your return type!
Enums Meet JSON
Here comes the Freezed packageâa best friend for anyone working with enums and JSON. Freezed lets you serialize and deserialize enums with ease, so you donât have to keep reinventing the wheel (or crying into your keyboard).
Freezed + Enums = BFFs
@JsonEnum(alwaysCreate: true)
enum Gender {
@JsonValue("none") none,
@JsonValue("male") male,
@JsonValue("female") female,
@JsonValue("others") others,
}
@freezed
class User with _$User {
const factory User({
required String id,
required String name,
required Gender gender,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
Serialize and Deserialize
With this setup:
From JSON to Dart:
final user = User.fromJson({"id": "123", "name": "Alice", "gender": "female"}); print(user.gender); // Outputs: Gender.femaleFrom Dart to JSON:
final json = user.toJson(); print(json); // Outputs: {"id": "123", "name": "Alice", "gender": "female"}
Enums Are Your Friends
Letâs recap:
Enums Simplify Your Life: They provide type safety and keep your code clean.
Theyâre Flexible: Add custom properties to make them more useful.
Freezed Makes Them JSON-Ready: No manual mapping required.
They Can Play Well with Lists: Use filtering and
firstWhereto find exactly what you need.
Enums Arenât Just CodeâTheyâre a Vibe
Think of enums as the party planners of your code. They manage whoâs invited (the fixed values) and keep the chaos at bay. Sure, they might not be as flashy as a fancy UI widget, but trust me, your enums are doing the heavy lifting behind the scenes. So next time you declare an enum, give it a little nod of respectâitâs earned it.
Happy coding! May your enums always be in sync with your JSON. đ