【发布时间】:2020-06-06 12:38:58
【问题描述】:
我在飞镖中有以下课程...
class Color {
final int value;
const Color._internal(this.value);
static const Color WHITE = const Color._internal(0);
static const Color BLACK = const Color._internal(1);
int get hashCode => value;
String toString() => (this == WHITE) ? 'w' : 'b';
}
当我运行颤振分析命令时,我收到以下错误...
信息 • 覆盖
==如果覆盖hashCode• lib/chess.dart:1584:13 • hash_and_equals
作为测试,我添加了方法
bool operator==(Object o) => true;
但后来我遇到了另一个分析器问题....
错误 • 常量映射条目键表达式类型“颜色”不能 覆盖 == 运算符 • lib/chess.dart:44:3 • const_map_key_expression_type_implements_equals
这是因为 Color 对象被用作静态常量映射中的键...
static const Map<Color, List> PAWN_OFFSETS = const {
BLACK: const [16, 32, 17, 15],
WHITE: const [-16, -32, -17, -15]
};
所以我对如何防止这些分析器问题感到困惑。
如果我删除 hashCode,该类将无法在地图中工作... 如果我添加 == 运算符,我只会得到一个不同的错误。
什么是最好的选择?
【问题讨论】: