Flutter Enum

What is enum ?

  • Enum in dart is enumerated, iterable
  • Enum is used for defining named constant values.

How to use ?

  • Synstax
enum PlatformEnum {
    android, ios, windown
}
  • Iterate over enum values
PlatformEnum.values.forEach((value) => print('value: $value, index: ${value.index}'));
  • Extension enum
extension PlatformEnumExtension on PlatformEnum {
    String? get title {
        switch(this) {
            case PlatformEnum.android:
                return 'Android';
            case PlatformEnum.ios:
                return 'iOS';
            case PlatformEnum.windown:
                return 'Windown';
            default:
                return null;
        }
    }
}

Comments !

Links

Social