Last active
March 12, 2025 15:06
-
-
Save nerder/f12cb9293ac2e92099c9c7a7ae2a20a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MembershipStatusEnhanced { | |
active(name: "active", value: 1), | |
trialing(name: "trialing", value: 2), | |
pastDue(name: "past_due", value: 3), | |
unpaid(name: "unpaid", value: 4), | |
inactive(name: "inactive", value: 0); | |
const MembershipStatusEnhanced({required this.name, required this.value}); | |
final String name; | |
final int value; | |
} | |
extension MembershipStatusParser on String { | |
MembershipStatusEnhanced toMembershipStatus() => MembershipStatusEnhanced.values.firstWhere( | |
(status) => status.name == this, | |
orElse: () => MembershipStatusEnhanced.inactive, | |
); | |
} | |
extension MembershipStatusParserFromInt on int { | |
MembershipStatusEnhanced toMembershipStatus() => MembershipStatusEnhanced.values.firstWhere( | |
(status) => status.value == this, | |
orElse: () => MembershipStatusEnhanced.inactive, | |
); | |
} | |
void main() { | |
print("active".toMembershipStatus()); | |
print("past_due".toMembershipStatus()); | |
print("not_supported".toMembershipStatus()); | |
print(1.toMembershipStatus()); | |
print(3.toMembershipStatus()); | |
print(5.toMembershipStatus()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment