-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from netglade/feat/more-extensions
Add a few new useful extensions
- Loading branch information
Showing
8 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
packages/netglade_utils/lib/src/extensions/enum_extensions.dart
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extension EnumExtensions<T extends Enum> on Iterable<T> { | ||
/// Finds the enum value in this list with name [name]. | ||
/// | ||
/// Goes through this collection looking for an enum with | ||
/// name [name], as reported by [EnumName.name]. | ||
/// Returns the first value with the given name. Such a value must be found. | ||
T byNameOrDefault(String name, {required T defaultValue}) { | ||
try { | ||
return byName(name); | ||
// ignore: avoid_catching_errors, byName throws it | ||
} on ArgumentError catch (_) { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
T? byNameOrNull(String name) { | ||
try { | ||
return byName(name); | ||
// ignore: avoid_catching_errors, byName throws it | ||
} on ArgumentError catch (_) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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