A Dart library with a solution to use smart enums

Smart Enums

Author: Jop Middelkamp

A package that can help you create ‘smarter’ enums that could be extended with some domain logic.

Usage

final bestFramework = Framework.flutter;

print('${bestFramework == Framework.flutter}'); // Prints 'true'
print('${bestFramework == Framework.maui}'); // Prints 'false'

class Framework extends SmartEnum<Framework> {
  static const Framework flutter = Framework._(
    1,
    name: 'Flutter',
    isCrossPlatform: true,
    score: 10,
  );
  static const Framework maui = Framework._(
    2,
    name: 'MAUI',
    isCrossPlatform: true,
    score: 8,
  );
  static const Framework reactNative = Framework._(
    4,
    name: 'React Native',
    isCrossPlatform: true,
    score: 9,
  );

  const Framework._(
    int value, {
    required this.name,
    required this.isCrossPlatform,
    required this.score,
  }) : super(value);

  final String name;
  final bool isCrossPlatform;
  final int score;

  static List<Framework> get crossPlatformFrameworks => const [
        Framework.flutter,
        Framework.maui,
        Framework.reactNative,
      ].where((f) => f.isCrossPlatform).toList();
}

Note

Inspired by Steve Smith‘s smart enum .NET library.

GitHub

View Github