Dart enum Extension
Easy Dart Enum
Made by Benjamin Mahmić (https://github.com/BenjaminMahmic)
How to use
- Select complete enum
- Call Quick Fix (Windows:
Ctrl + . or Mac: ⌘ + . ) and select Generate (is)Enum Extension
- That's it! 🎉
Example
enum SubscriptionType {
free,
monthly,
annual,
}
extension SubscriptionTypeExtension on SubscriptionType {
bool get isFree => this == SubscriptionType.free;
bool get isMonthly => this == SubscriptionType.monthly;
bool get isAnnual => this == SubscriptionType.annual;
}
final subscriptionType = SubscriptionType.monthly;
void main() {
// Now
print(subscriptionType.isMonthly); // true
print(subscriptionType.isAnnual); // false
// Before
print(subscriptionType == SubscriptionType.monthly); // true
print(subscriptionType == SubscriptionType.annual); // false
// Now
if (subscriptionType.isMonthly) {
print('Monthly');
} else if (subscriptionType.isAnnual) {
print('Annual');
} else {
print('Free');
}
// Before
if (subscriptionType == SubscriptionType.monthly) {
print('Monthly');
} else if (subscriptionType == SubscriptionType.annual) {
print('Annual');
} else {
print('Free');
}
}
Enjoy! 🚀
| |