π₯What are #equality (==) and #hashCode in #Dart / #Flutter objects and why would you need to override these? π A thread to explain it all π§΅
ππ»1/14
Object equality in Dart is defined through the == operator. This is an operator that has a return value of bool and has only one parameter which is another object of the same type this operator is implemented on
2/14
The == operator's implementation in Dart has to have 4 important qualities.
Quality #1 is that this operator should be "total" meaning that it should only return a boolean and it should never throw
3/14
Quality #2 is that it should be "reflexive" meaning that for the same object instance, it should return true. So if you instantiate an object and compare it to itself, it should always return true.
4/14
Quality #3 is that it should be "symmetric" meaning that if object 1 is equal to object 2, then object 2 should also be equal to object 1 in the reverse manner
5/14
Quality #4 of the == operator and hence the last quality is that it should be "transitive" meaning that if object 1 is equal to object 2 and object 2 is equal to object 3 then object 1 should also be equal to object 3
6/14
The default implementation of the == operator only implements quality #1 and #2 meaning that it never throws and it only checks for the objects being compared to be the same instance, or to be "identical" in other words
7/14
If you have a custom class, you want to always ensure that you are implementing the == operator on it. Let me show you an example
8/14
In this example, the LoginHandle class has 1 property called "token" of type "String". We've implemented the == operator to ensure that this.token == other.token. Note "covariant". This means our operator override always expects another instance of LoginHandle
9/14
When overriding this operator, ensure that the 4 qualities named above are met and that your parameter is a covariant of the same type as "this"
10/14
Equality implementation is only half of the equation for equality in Dart. When implementing equality with the == operator, you also need to ensure that you've implemented the hashCode property.
11/14
hashCode property is of type int. hashCode is an integer that represents the state of an object inside collections such as Map. Map in Dart is implemented so that it uses an object's hashCode to store that object inside itself
12/14
That means if two objects have the same hashCode, they are going to consume the same space in the map and over-write each other. Here is an example of two instances being equal and used as keys to a Map
13/14
So whereas the == operator is used to test for equality, the hashCode property is used to represent the state of an object. If two objects are equal from == operator, they should have the same hashCode
14/14
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
People on LinkedIn are there to expand their professional network. LinkedIn is not a dating app. Please keep your conversations only to a professional level 1/
When sending messages to a new contact, get to the point in your first message. Don't send a message like "Hi" and wait before actually saying what you want to say. I get about 200 "hi" messages per week! 2/
* StalessWidget comes from Widget > DiagnosticableTree > Diagnosticable. It defines a user interface by returning a Widget in its build function. 1/
In StatefulWidget and StatelessWidget, both, the build function receives a BuildContext using which you can retrieve information from up the build chain in order to complete rendering your widgets. 2/
Confused about #Provider in #Flutter? Thread to explain some of the most important concepts
π§΅ππ»
context.select() is used when you want to watch a specific aspect of a provider and rebuild your widget upon that value changing. It's only useful inside the build() function of your widgets and changes to the selected value will mark the widget as needing to be rebuilt
by using context.select(), you rebuild your widget only if the given aspect of your provider changes, this is specially useful if you have a large provider with many moving parts