Skip to content

Instantly share code, notes, and snippets.

@roipeker
Last active May 23, 2020 03:53
Show Gist options
  • Save roipeker/98cfe5985f923a0def0070c26fad5f08 to your computer and use it in GitHub Desktop.
Save roipeker/98cfe5985f923a0def0070c26fad5f08 to your computer and use it in GitHub Desktop.
Dart's variable hashCode to compare instances.
Future<void> main(List<String> arguments) async {
var a = Contacto('Mati');
var b = Contacto('Mati');
print('Contact son iguales? ${a == b}'); // false
var a2 = const Contacto('Mati');
var b2 = const Contacto('Mati');
print('const Contact son iguales? ${a2 == b2}'); // true
var a3 = Contacto2('Mati');
var b3 = Contacto2('Mati');
print('Contact2 son iguales? ${a3 == b3}'); // true
var list = <Contacto2>[];
list.add(a3);
print('Existe el mismo Contact2 en el List? ${list.contains(b3)}'); // true
}
class Contacto {
final String nombre;
const Contacto(this.nombre);
}
class Contacto2 {
String nombre;
Contacto2(this.nombre);
@override
bool operator ==(o) => o.runtimeType == runtimeType && nombre == o.nombre;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment