Dart can't find index of object in list with indexOf()

So, either I have a very weird problem here on my hand, or I'm just plain dumb.

I have a list of Objects. The Attributes of each object are a few doubles, an int and a String.

My problem is that list.indexOf(object) returns -1 (e.g. indexOf() could not find the element) even if there is an object in the list whose attributes are carbon copy of the object whose index I want to find.

Even list.indexOf(list[0]) returns -1, for some goddamn reason (Edit: Initially mistyped the -1 as 0). And yes, the list actually has data in it.

Here's the code of my object:

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()); } } 
4

1 Answer

In the end it turns out that I would have needed to add a method to compare objects, so my class looks like this in the end:

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}); @override bool operator ==(o) { if (o is! Standard) { return false; } else { return o.l == l && o.a == a && o.b == b && o.lUpperTolerance == lUpperTolerance && o.lLowerTolerance == lLowerTolerance && o.aUpperTolerance == aUpperTolerance && o.aLowerTolerance == aLowerTolerance && o.bUpperTolerance == bUpperTolerance && o.bLowerTolerance == bLowerTolerance && o.eTolerance == eTolerance && o.timestamp == timestamp && o.identStr == identStr; } } @override int get hashCode => super.hashCode; 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()); } } 
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like