Junior — Middle
Could you explain what a hash code is and what it is used for?
sobes.tech AI
Answer from AI
Hash code is a fixed-size numerical value calculated based on the content of an object. It is used for quick comparison and searching of objects, for example, in hash tables or dictionaries.
In Flutter (and Dart), the hashCode method helps efficiently store and search for objects in collections such as Set or Map. If two objects are equal (by the == method), their hash codes should match.
Example:
class Person {
final String name;
final int age;
Person(this.name, this.age);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Person && other.name == name && other.age == age;
@override
int get hashCode => name.hashCode ^ age.hashCode;
}