6 likes
·
817 reads
4 comments
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 '';
}
}
}
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?? '';
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.
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#.