|
| 1 | +import RuleDetails from '@site/src/components/RuleDetails'; |
| 2 | + |
| 3 | +<RuleDetails version="5.3.0" severity="warning" /> |
| 4 | + |
| 5 | +Warns when a field is not added to `props` getter of a class that extends `Equtable` or `EquatableMixin`. |
| 6 | + |
| 7 | +:::info |
| 8 | + |
| 9 | +This rule is specific to [`equatable` package](https://pub.dev/packages/equatable). Enable it only if you use this package. |
| 10 | + |
| 11 | +::: |
| 12 | + |
| 13 | +### Example {#example} |
| 14 | + |
| 15 | +**❌ Bad:** |
| 16 | + |
| 17 | +```dart |
| 18 | +class AnotherPerson extends Equatable { |
| 19 | + const AnotherPerson(this.name, this.age); |
| 20 | +
|
| 21 | + final String name; |
| 22 | +
|
| 23 | + final int age; |
| 24 | +
|
| 25 | + @override |
| 26 | + List<Object> get props => [name]; // LINT |
| 27 | +} |
| 28 | +
|
| 29 | +class AndAnotherPerson extends Equatable { |
| 30 | + static final someProp = 'hello'; |
| 31 | +
|
| 32 | + const AndAnotherPerson(this.name); |
| 33 | +
|
| 34 | + final String name; |
| 35 | +
|
| 36 | + @override |
| 37 | + List<Object> get props => [name]; |
| 38 | +} |
| 39 | +
|
| 40 | +class SubPerson extends AndAnotherPerson { |
| 41 | + const SubPerson(this.value, String name) : super(); |
| 42 | +
|
| 43 | + final int value; |
| 44 | +
|
| 45 | + @override |
| 46 | + List<Object> get props { |
| 47 | + return super.props..addAll([]); // LINT |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +class EquatableDateTimeSubclass extends EquatableDateTime { |
| 52 | + final int century; |
| 53 | +
|
| 54 | + EquatableDateTimeSubclass( |
| 55 | + this.century, |
| 56 | + int year, [ |
| 57 | + int month = 1, |
| 58 | + int day = 1, |
| 59 | + int hour = 0, |
| 60 | + int minute = 0, |
| 61 | + int second = 0, |
| 62 | + int millisecond = 0, |
| 63 | + int microsecond = 0, |
| 64 | + ]) : super(year, month, day, hour, minute, second, millisecond, microsecond); |
| 65 | +
|
| 66 | + @override |
| 67 | + List<Object> get props => super.props..addAll([]); // LINT |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +**✅ Good:** |
| 72 | + |
| 73 | +```dart |
| 74 | +class AnotherPerson extends Equatable { |
| 75 | + const AnotherPerson(this.name, this.age); |
| 76 | +
|
| 77 | + final String name; |
| 78 | +
|
| 79 | + final int age; |
| 80 | +
|
| 81 | + @override |
| 82 | + List<Object> get props => [name, age]; |
| 83 | +} |
| 84 | +
|
| 85 | +class AndAnotherPerson extends Equatable { |
| 86 | + static final someProp = 'hello'; |
| 87 | +
|
| 88 | + const AndAnotherPerson(this.name); |
| 89 | +
|
| 90 | + final String name; |
| 91 | +
|
| 92 | + @override |
| 93 | + List<Object> get props => [name]; |
| 94 | +} |
| 95 | +
|
| 96 | +class SubPerson extends AndAnotherPerson { |
| 97 | + const SubPerson(this.value, String name) : super(); |
| 98 | +
|
| 99 | + final int value; |
| 100 | +
|
| 101 | + @override |
| 102 | + List<Object> get props { |
| 103 | + return super.props..addAll([value]); |
| 104 | + } |
| 105 | +} |
| 106 | +
|
| 107 | +class EquatableDateTimeSubclass extends EquatableDateTime { |
| 108 | + final int century; |
| 109 | +
|
| 110 | + EquatableDateTimeSubclass( |
| 111 | + this.century, |
| 112 | + int year, [ |
| 113 | + int month = 1, |
| 114 | + int day = 1, |
| 115 | + int hour = 0, |
| 116 | + int minute = 0, |
| 117 | + int second = 0, |
| 118 | + int millisecond = 0, |
| 119 | + int microsecond = 0, |
| 120 | + ]) : super(year, month, day, hour, minute, second, millisecond, microsecond); |
| 121 | +
|
| 122 | + @override |
| 123 | + List<Object> get props => super.props..addAll([century]); |
| 124 | +} |
| 125 | +``` |
0 commit comments