Freezed: Support enums

Created on 27 Feb 2020  路  9Comments  路  Source: rrousselGit/freezed

Add support for when/map on custom enums:

@freezed
abstract class Enum with _$Enum {
  const factory Enum._(String label) = _Enum;

  static const first = Enum._('1st');
  static const second = Enum._('2nd');

  static const values = _$EnumValues;

  factory Enum.fromJson(Map<String, dynamic> json) => _$EnumFromJson(json);
}

which then allows

Enum enum = Enum.first;

enum.when(
  first: (label) {},
  second: (label) {},
);
enhancement

All 9 comments

This should also make it such that the deserialization does not rely on the contained value either.

Such that changing

static const first = Enum._('1st');

to

static const first = Enum._('first');

won't be a breaking change.

Do we need a new decorator for this, or is @freezed enough?

I would assume that we don't need one. It could be inferred based on:

  • the class has a single private constructor (besides the toJson)
  • it has a static values
  • it has at least one static final/static const using that class
  • it applies only to static final/const. Static mutable variables are ignored

Flags enums are welcome

Enum enum = Enum.first + Enum.second;
if (enum.has(Enum.first)){
***
}

That would complexify quite a fair bit the serialization/deserialization process and reduce the readability.

In the end, you can do:

Set<Enum> enums = {Enum.first, Enum.second};
if (enums.contains(Enum.first)) {

}

Do we need a new decorator for this, or is @freezed enough?

I would assume that we don't need one. It could be inferred based on:

  • the class has a single private constructor (besides the toJson)
  • it has a static values
  • it has at least one static final/static const using that class
  • it applies only to static final/const. Static mutable variables are ignored

Adding a separate annotation like @freezed_enum would be better. So user can declare enums explicitly and generator can check for the correct code structure.

And it would be nice if we can also declare a fallback enum value to be used when json input is unknown.

I am certain my question is out of place here, but I can't seem to find an answer elsewhere. (and I don't feel it deserves it's own ticket)

when parsing an enum field, how do I allow the the full string value to come from the Json object.

enum Gender {
male, female,
}
...
factory Person(
Gender gender
);
...
Person(Gender.male); /// Throws error that 'Gender.male' is not one of the values: male, female

How can I use the full string value Gender.male?

How about making the same implementation as stated in this comment https://github.com/rrousselGit/freezed/issues/191#issuecomment-638842201
Or are there problems with json_serializable's json to enum values?

Hi @rrousselGit

How are you planning to implement enums in freezed? Are you planning to implement it in the near future or is it going to take more time?

This is the last feature that would allow freezed to cover a major part of the general use cases 馃挴

That's not in my priorities.

If someone wants to tackle it, feel free to open a PR. I can answer questions. But at the moment, my focus is on other things (devtools for Provider/Riverpod/hooks)

Was this page helpful?
0 / 5 - 0 ratings