My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Loïc Yabili

6 likes

·

817 reads

4 comments

Yohanes Bandung
Yohanes Bandung
Oct 4, 2023

Great information!

We get the ability to add default value if mapping to enum use non-assigned value.

do you have any idea how we can get default value if the enum is nullable? for example:

final WeekDay? weekDay = null;

// I want to get empty string
final String weekDayShortName = weekDay.shortName;

previously, I use something like this:

enum WeekDay {
  monday
}

// extend nullable WeekDay
extension WeekDayExtension on WeekDay? {
  String get shortName {
    switch (this) {
      case WeekDay.monday:
        return 'Mon';
      case null:
        return '';
    }
  }
}
1
·
·1 reply
Loïc Yabili
Loïc Yabili
Author
·Oct 4, 2023

Thank you for your comment.

If the enum is nullable it can't be used without the null aware operator '?', for me there is not need to add a case for that, you can directly use:

final String weekDayShortName = weekDay?.shortName?? '';

·
Diego Ponce de León
Diego Ponce de León
Oct 7, 2023

I'm trying to digest Dart right now (coming from C#) and this particular enum thing is more than awesome. I have many ideas to implement your example.

However I'm missing enum flags available in other languages. Turns out there is a library though: pub.dev/packages/enum_flag but I think it should be part of the language.

1
·
·1 reply
Loïc Yabili
Loïc Yabili
Author
·Oct 9, 2023

Thank you Diego for your feedback.

For the moment the enum flag is not natively integrated in Dart, I agree with you, it will be a great option like on C#.

·