【发布时间】:2021-08-25 10:08:17
【问题描述】:
所以,要么我手头有一个非常奇怪的问题,要么我就是个笨蛋。
我有一个对象列表。每个对象的 Attributes 是几个 double,一个 int 和一个 String。
我的问题是 list.indexOf(object) 返回 -1(例如 indexOf() 找不到元素),即使列表中有一个对象,其属性是我要查找其索引的对象的副本。
即使list.indexOf(list[0]) 返回 -1,出于某种该死的原因(编辑:最初将 -1 错误输入为 0)。是的,列表中实际上包含数据。
这是我的对象的代码:
class Standard {
double l;
double a;
double b;
double lUpperTolerance;
double lLowerTolerance;
double aUpperTolerance;
double aLowerTolerance;
double bUpperTolerance;
double bLowerTolerance;
double eTolerance;
int timestamp;
String identStr;
Standard(
{this.l,
this.a,
this.b,
this.lUpperTolerance,
this.lLowerTolerance,
this.aUpperTolerance,
this.aLowerTolerance,
this.bUpperTolerance,
this.bLowerTolerance,
this.eTolerance,
this.timestamp,
this.identStr});
factory Standard.fromJson(Map<String, dynamic> jsonData) {
if (jsonData != null) {
double l = (jsonData['l'] ?? 0.0) as double;
double a = (jsonData['a'] ?? 0.0) as double;
double b = (jsonData['b'] ?? 0.0) as double;
double lUpperTolerance = (jsonData['lUpperTolerance'] ?? 1.0) as double;
double lLowerTolerance = (jsonData['lLowerTolerance'] ?? 1.0) as double;
double aUpperTolerance = (jsonData['aUpperTolerance'] ?? 1.0) as double;
double aLowerTolerance = (jsonData['aLowerTolerance'] ?? 1.0) as double;
double bUpperTolerance = (jsonData['bUpperTolerance'] ?? 1.0) as double;
double bLowerTolerance = (jsonData['bLowerTolerance'] ?? 1.0) as double;
double eTolerance = (jsonData['eTolerance'] ?? 1.0) as double;
int timestamp = (jsonData['timestamp'] ?? 1500000000) as int;
String identStr = (jsonData['identStr'] ?? '0') as String;
return Standard(
l: l,
a: a,
b: b,
lUpperTolerance: lUpperTolerance,
lLowerTolerance: lLowerTolerance,
aUpperTolerance: aUpperTolerance,
aLowerTolerance: aLowerTolerance,
bUpperTolerance: bUpperTolerance,
bLowerTolerance: bLowerTolerance,
eTolerance: eTolerance,
timestamp: timestamp,
identStr: identStr);
} else {
return null;
}
}
Map<String, dynamic> toJson() => {
'l': l,
'a': a,
'b': b,
'lUpperTolerance': lUpperTolerance,
'lLowerTolerance': lLowerTolerance,
'aUpperTolerance': aUpperTolerance,
'aLowerTolerance': aLowerTolerance,
'bUpperTolerance': bUpperTolerance,
'bLowerTolerance': bLowerTolerance,
'eTolerance': eTolerance,
'timestamp': timestamp,
'identStr': identStr,
};
void dump() {
print('l: ' + this.l.toString());
print('a: ' + this.a.toString());
print('b: ' + this.b.toString());
print('lUpper: ' + this.lUpperTolerance.toString());
print('lLower: ' + this.lLowerTolerance.toString());
print('aUpper: ' + this.aUpperTolerance.toString());
print('aLower: ' + this.aLowerTolerance.toString());
print('bUpper: ' + this.bUpperTolerance.toString());
print('bLower: ' + this.bLowerTolerance.toString());
print('eTolerance: ' + this.eTolerance.toString());
print('timestamp: ' + this.timestamp.toString());
print('identStr: ' + this.identStr.toString());
}
}
【问题讨论】:
-
你能显示你的对象的代码吗?您确定没有覆盖对象中的 == 运算符吗?
-
@smotastic 将其添加到问题的末尾。除了我想用它来查找对象索引的位置之外,我在任何代码中都找不到任何提及 indexOf 的内容,并且 indexOf 上的 Ctrl + Q 显示了来自
list.dart的帮助 -
indexOf取决于对象相等性。如果您没有为您的对象提供operator ==,那么默认的相等概念就是对象标识。但是,list.indexOf(list[0])不应该返回 -1,如果那是您真正输入的内容。请提供演示您遇到的问题的重现代码。 -
因为我不太确定我是否真的偶然发现了这里可能被视为错误的地方,所以我实际上将那行确切的行放入了我的代码中并运行了它。但是,是的,它缺少一个相等运算符。