Flutter == 和 identical 的区别

1,943 阅读1分钟

Flutter == 和 identical的区别

==

通过对象的 operator ==比较是否相等.

identical

通过比较两个引用的是否是同一个对象判断是否相等.

demo

main(List<String> args) {
  var o = Demo(1);
  var o2 = Demo(1);

  print(o == o2);
  print(identical(o, o2));
}

class Demo {
  int index;
  Demo(this.index);

  bool operator ==(other) {
    return (other is Demo && other.index == index);
  }
}
----------------------------
//true
//false