Last active
May 23, 2020 03:53
-
-
Save roipeker/98cfe5985f923a0def0070c26fad5f08 to your computer and use it in GitHub Desktop.
Dart's variable hashCode to compare instances.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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