Skip to main content

prefer-enums-by-name

added in: 1.6.0
Free+

Since Dart 2.15 it's possible to use byName method on enum values prop instead of searching the value with firstWhere.

warning

byName will throw an exception if the enum does not contain a value for the given name.

Example

❌ Bad:

// LINT: Prefer 'values.byName' instead of '.firstWhere'.
final styleDefinition = StyleDefinition.values.firstWhere(
(def) => def.name == json['styleDefinition'],
);

✅ Good:

final styleDefinition = StyleDefinition.values.byName(json['styleDefinition']);