prefer_equatable_mixin
v0.4.0 Warning Fix Type Annotations
Flags a class that extends Equatable from package:equatable instead of mixing in EquatableMixin. Both give the same value equality; only one leaves the extends slot free.
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 EquatableMixin { 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”With the mixin, 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 EquatableMixin { 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 EquatableMixin { const BaseEntity();}Not reported
Section titled “Not reported”Only extends Equatable is reported. A class that already uses the mixin, 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()’.