DART:实例列表中的 indexOf()
DART: indexOf() in a list of Instances
如何获取List中某个Instance的索引?
class Points {
int x, y;
Point(this.x, this.y);
}
void main() {
var pts = new List();
int Lx;
int Ly;
Points pt = new Points(25,55); // new instance
pts.add(pt);
int index = pts.indexOf(25); // Problem !!! How to obtain the index in a list of instances ?
if (index != -1 ){
Lx = lp1.elementAt(index).x;
Ly = lp1.elementAt(index).y;
print('X=$Lx Y=$Ly');
}
// some helper to satisfy `firstWhere` when no element was found
var dummy = new Point(null, null);
var p = pts.firstWhere((e) => e.x == 25, orElse: () => dummy);
if(p != dummy) {
// don't know if this is still relevant to your question
// the lines above already got the element
var lx = pts[pts.indexOf(p)];
print('x: ${lx.x}, y: ${lx.y}');
}
如何获取List中某个Instance的索引?
class Points {
int x, y;
Point(this.x, this.y);
}
void main() {
var pts = new List();
int Lx;
int Ly;
Points pt = new Points(25,55); // new instance
pts.add(pt);
int index = pts.indexOf(25); // Problem !!! How to obtain the index in a list of instances ?
if (index != -1 ){
Lx = lp1.elementAt(index).x;
Ly = lp1.elementAt(index).y;
print('X=$Lx Y=$Ly');
}
// some helper to satisfy `firstWhere` when no element was found
var dummy = new Point(null, null);
var p = pts.firstWhere((e) => e.x == 25, orElse: () => dummy);
if(p != dummy) {
// don't know if this is still relevant to your question
// the lines above already got the element
var lx = pts[pts.indexOf(p)];
print('x: ${lx.x}, y: ${lx.y}');
}