Created with <3 with dartpad.dev.
Created
May 23, 2023 11:04
-
-
Save cleong98/b41f41b413863c571a738f0964fc69c4 to your computer and use it in GitHub Desktop.
try-enum
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
| void main() { | |
| final p = PortType.typeA; | |
| print(PortType.fromString('123')); | |
| print(PortType.fromString('Type-C')); | |
| print(p.isUsb); | |
| } | |
| enum PortType { | |
| typeA('Type-A'), | |
| typeC('Type-C'), | |
| lightning('LIGHTNING'); | |
| final String name; | |
| const PortType(this.name); | |
| static PortType? fromString(String name) { | |
| for(final value in values) { | |
| if (value.name == name) { | |
| return value; | |
| } | |
| } | |
| return null; | |
| } | |
| } | |
| extension PortTypeExtension on PortType { | |
| bool get isUsb => name.startsWith('USB') || this == PortType.typeA || this == PortType.typeC; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment