This requires the enhanced-enums language feature to be enabled

Viewed 27

everyone. I have this error when I try to load Enum. I'm using SDK 2.7.0 & Flutter 3.0.4. I don't want to update my SDK, but I need another solution. Could you please help me with this error.

enum MessageEnum {
  text('text'),
  image('image'),

  const MessageEnum(this.type);
  final String type;
}

// Using an extension
// Enhanced enums

extension ConvertMessage on String {
  MessageEnum toEnum() {
    switch (this) {
      case 'image':
        return MessageEnum.image;
      case 'text':
        return MessageEnum.text;
      default:
        return MessageEnum.text;
    }
  }
}
1 Answers

Enhanced enums is a feature introduced in Dart version 2.17.0. Though in your example I actually don't quite understand why you even need enhanced enums. What exactly is the reason for the type field? Since it's exactly the same as the name. Can't you just do this?:

enum MessageEnum { text, image }

extension ConvertMessage on String {
  MessageEnum toEnum() {
    switch (this) {
      case 'image':
        return MessageEnum.image;
      case 'text':
        return MessageEnum.text;
      default:
        return MessageEnum.text;
    }
  }
}
Related