byNameTry method
Finds and returns the first enum value in this list whose name
property
matches the given name
, or null
if no such value is found.
If caseSensitive
is true
, the comparison between the name
property
of each enum value and the given name
is case-sensitive. Otherwise,
the comparison is case-insensitive.
-
Parameters:
- name: The name to search for among the enum values.
- caseSensitive: Whether the comparison should be case-sensitive.
-
Returns: The first enum value whose
name
matches the givenname
, ornull
if no such value is found.
Implementation
T? byNameTry(String? name, {bool caseSensitive = true}) {
if (name == null || name.isEmpty) {
return null;
}
// Use a local variable to store the name for comparison
final String comparisonName = caseSensitive ? name : name.toLowerCase();
// Find the first enum value whose name matches the provided name
return firstWhereOrNull(
(T e) => caseSensitive ? e.name == comparisonName : e.name.toLowerCase() == comparisonName,
);
}