flutter - 检查列表中对象的可用性 -Dart
问题描述
这是我的课
class TypeId {
final int id;
final int type;
TypeId(this.id, this.type);
}
在主函数中
void main() {
List<TypeId> typeIds = [
TypeId(1, 2),
TypeId(1, 3),
];
TypeId sample = TypeId(1, 3);
if (typeIds.contains(sample)) {
print('Its Working');
} else {
print('This Sucks');
}
}
我不知道为什么它不比较,如果这是错误的,请告诉如何检查
解决方案
必须进行比较,因为对象不一样。
class TypeId {
final int id;
final int type;
TypeId(this.id, this.type);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is TypeId &&
other.id == id &&
other.type == type;
}
@override
int get hashCode => id.hashCode ^ type.hashCode;
}
推荐阅读
- google-apps-script - 库中带有脚本功能的 HTML 在另一个项目中不起作用
- python-sphinx - Sphinx 没有正确格式化重载的 Python 函数参数
- javascript - Redis 键替代
- azure - Azure DataFactory:计划管道的开始/结束时间
- ios - BGProcessingTask 由于信号 9 而终止
- c++ - 返回类型的模板参数推导
- python - Python 有偏样本分布
- php - 由于 php 中的 URL 注入导致信息泄露
- javascript - SyntaxError:在 require("file_path") 语句上出现意外的标记 '<'
- r - 如何将文本粘贴到 R 中 ggplot2 图中的数字 y 轴刻度上?