Skip to content

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];
}

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];
}

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();
}
// Do
abstract class BaseEntity with EquatableMixin {
const BaseEntity();
}

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

This rule is in no preset, so it is off unless you enable it by name:

many_lints.yaml
rules:
prefer_equatable_mixin: true

To turn it off again:

many_lints.yaml
rules:
prefer_equatable_mixin: false

To keep the rule on but skip certain paths, use per-rule exclude.