prefer_equatable_mixin
v0.4.0 Warning Fix Type Annotations
Flags a class that extends Equatable from package:equatable instead of mixing it in. Both give the same value equality; only one leaves the extends slot free.
What to mix in depends on the equatable version your package resolves, and the message and quick fix follow it:
| equatable | Mix in |
|---|---|
| 2.1 and later, including 3.0 | with Equatable. 2.1 deprecated EquatableMixin and 3.0 removed it. |
| 2.0 | with Equatable. Equatable is not a mixin class there. |
Dart allows a single superclass. Spending it on Equatable means the day this
model needs a real base class, the equality has to be reworked first:
import 'package:equatable/equatable.dart';
class CartItem extends Equatable { const CartItem(this.sku, this.quantity);
final String sku; final int quantity;
@override List<Object?> get props => [sku, quantity];}import 'package:equatable/equatable.dart';
class CartItem with Equatable { CartItem(this.sku, this.quantity);
final String sku; final int quantity;
@override List<Object?> get props => [sku, quantity];}The point: extends stays available
Section titled “The point: extends stays available”Mixed in, the same model can join a hierarchy without giving up equality:
import 'package:equatable/equatable.dart';
abstract class DomainEntity { const DomainEntity(this.id);
final String id;}
class CartItem extends DomainEntity with Equatable { CartItem(super.id, this.sku, this.quantity);
final String sku; final int quantity;
@override List<Object?> get props => [id, sku, quantity];}Abstract bases too
Section titled “Abstract bases too”An abstract class that extends Equatable passes the same constraint on to
every subclass:
import 'package:equatable/equatable.dart';
// Don't — every subclass now has its `extends` slot spent.abstract class BaseEntity extends Equatable { const BaseEntity();}
// Doabstract class BaseEntity with Equatable { const BaseEntity();}Not reported
Section titled “Not reported”Only extends Equatable is reported. A class that already mixes it in, and
a class extending something else entirely, are left alone.
See also: equatable package
Configuration
Section titled “Configuration”This rule is in no preset, so it is off unless you enable it by name:
rules: prefer_equatable_mixin: trueTo turn it off again:
rules: prefer_equatable_mixin: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”list_all_equatable_fields— Ensure all fields are listed in Equatable props.avoid_collection_equality_checks— Avoid comparing collections with == or != as it checks reference equality, not contents.prefer_overriding_parent_equality— Override == and hashCode when the parent class overrides them.prefer_async_callback— Use ‘AsyncCallback’ instead of ‘Future<void> Function()’.