DEV Community

Cover image for Flutter/Dart Tips - Enhanced enums
asuna24
asuna24

Posted on

Flutter/Dart Tips - Enhanced enums

Have you ever used enum before ? Enum defined as "a type of data where only a set of predefined values exist". In Dart, enum is a special kind of class that used to represent constant values.

For example, we want to have predefined constant value that represent something. Lets say we want to have a type to define rainbow color.. as we know, rainbow colors consist of red, orange, yellow, green, blue, indigo, and violet. Each of these colors can have its Color value, label, and index.

With normal enum type, we can create something like this :

enum RainbowColor {
  red, 
  orange, 
  yellow, 
  green, 
  blue, 
  indigo, 
  violet
}

extension RainbowColorExt on RainbowColor {
  static Map<RainbowColor, Color> mapRainbowToColor = {
    RainbowColor.red: Colors.red,
    RainbowColor.orange: Colors.orange,
    RainbowColor.yellow: Colors.yellow,
    RainbowColor.green: Colors.green,
    RainbowColor.blue: Colors.blue,
    RainbowColor.indigo: Colors.indigo,
    RainbowColor.violet: Colors.purple
  };

  Color getColor() {
    return mapRainbowToColor[this]!;
  }

  static Map<RainbowColor, String> mapRainbowToLabel = {
    RainbowColor.red: 'Red',
    RainbowColor.orange: 'Orange',
    RainbowColor.yellow: 'Yellow',
    RainbowColor.green: 'Green',
    RainbowColor.blue: 'Blue',
    RainbowColor.indigo: 'Indigo',
    RainbowColor.violet: 'Violet'
  };

  String getLabel() {
    return mapRainbowToLabel[this]!;
  }
}
Enter fullscreen mode Exit fullscreen mode

Here, we created RainbowColor enum with its defined extension and functions. To receive the label and color, we can do something like this :

const RainbowColor red = RainbowColor.red;
final String redLabel = red.getLabel();
final Color redColor = red.getColor();
Enter fullscreen mode Exit fullscreen mode

But you see that we need to connect our enum by using extension to get its corresponding label and color.

Since Dart 2.17, they showed us new way to provide something like above.. by using Enhanced Enum.

Enhanced Enum

Dart also allows enum declarations to declare classes with fields, methods, and const constructors which are limited to a fixed number of known constant instances.

This mean that we can create an enum to just like creating a special class that consist of some constants inside. See below :

enum RainbowColor {
  red(Colors.red, 'Red'),
  orange(Colors.orange, 'Orange'),
  yellow(Colors.yellow, 'Yellow'),
  green(Colors.green, 'Green'),
  blue(Colors.blue, 'Blue'),
  indigo(Colors.indigo, 'Indigo'),
  violet(Colors.purple, 'Violet');

  const RainbowColor(Color this.color, String this.label);
  final Color color;
  final String label;
}
Enter fullscreen mode Exit fullscreen mode

to get corresponding color and label, we can just use this :

const RainbowColor red = RainbowColor.red;
final Color redColor = red.color;
final String redLabel = red.label;
Enter fullscreen mode Exit fullscreen mode

Other than that, we also can create a function for this specific enum :

enum RainbowColor {
  red(Colors.red, 'Red'),
  orange(Colors.orange, 'Orange'),
  yellow(Colors.yellow, 'Yellow'),
  green(Colors.green, 'Green'),
  blue(Colors.blue, 'Blue'),
  indigo(Colors.indigo, 'Indigo'),
  violet(Colors.purple, 'Violet');

  const RainbowColor(Color this.color, String this.label);
  final Color color;
  final String label;

  // New Function
  String toString() {
    return 'This color is $label';
  }
}

const RainbowColor red = RainbowColor.red;
final Color redColor = red.color;
final String redLabel = red.label;

// return : "This color is Red"
final String result = red.toString();
Enter fullscreen mode Exit fullscreen mode

This update really helped us to reduce our code and simplify our way to create enum for our Flutter/Dart App.

Conclusion

Enum helps us to predefine constant values to represent something in our app. Previously, to create some functionality for our enum we need to create an extension with longer way to achieve our needs. Since Dart 2.17, Dart introduced Enhanced Enum which can help us to reduce our code to define enum with its functionality. Just like how we can define class with attributes and functions.

Cheers!

Top comments (0)