valueOf method
Returns the enum value from this iterable that matches the given string value
.
The comparison is case-insensitive. Returns null
if no match is found.
This is commonly used with MyEnum.values
.
{@tool snippet}
enum Status { pending, active, done }
void main() {
// Successful lookup (case-insensitive)
final status = Status.values.valueOf('ACTIVE');
print(status); // Prints: Status.active
// Failed lookup
final unknown = Status.values.valueOf('archived');
print(unknown); // Prints: null
}
{@end-tool}
Implementation
@pragma('vm:prefer-inline')
T? valueOf(String? value) {
return firstWhereOrNull(
(type) => type.name.toLowerCase() == value?.toLowerCase().trim(),
);
}