Skip to content

Instantly share code, notes, and snippets.

@cleong98
Created May 23, 2023 11:04
Show Gist options
  • Save cleong98/b41f41b413863c571a738f0964fc69c4 to your computer and use it in GitHub Desktop.
Save cleong98/b41f41b413863c571a738f0964fc69c4 to your computer and use it in GitHub Desktop.
try-enum
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