dart - 我可以在地图键中使用对象吗?
问题描述
我有一张像这样的地图:
Map<Coordinates, Offset> myMap = {someCoordinates: someOffset, ...};
我想通过坐标得到一个偏移量(其中坐标是一个简单的类,带有 int x 和 int y)
...
Offset someOffset = myMap[someCoordinaets];
但它返回null。我做错了什么?我可以在地图键中使用对象吗?
编辑 这里是我的坐标类
class Coordinates {
final int col;
final int row;
Coordinates(this.col, this.row);
int getCol() {
return col;
}
int getRow() {
return row;
}
}
解决方案:
import 'package:quiver/core.dart';
class Coordinates {
final int col;
final int row;
Coordinates(this.col, this.row);
// solution
bool operator ==(o) => o is Coordinates && col == o.col && row == o.row;
int get hashCode => hash2(col.hashCode, row.hashCode);
int getCol() {
return col;
}
int getRow() {
return row;
}
}
解决方案
推荐阅读
- python - 熊猫样式:按绝对值有条件地更改列的背景颜色
- asp.net-core - 无法在 asp.net 操作过滤器中获取自定义属性
- c++ - c ++将值设置为特定地址
- pine-script - 如何在 plotchar 函数调用中使用可变变量?
- c# - WaitHandleCannotBeOpenedException 和 MMF FileNoteFoundException 杂音
- matlab - 如何检验相关性?
- javascript - 为什么 take 会触发 Observable?
- node.js - Google oAuth2 - consent screen and redundant popup
- android - Printer Android - 打印机在行首打印点
- python - h5py中的双端队列