Vandad Nahavandipoor Profile picture
Mar 25 β€’ 14 tweets β€’ 4 min read
πŸ”₯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 Image
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 Image
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
γ€€

Keep Current with Vandad Nahavandipoor

Vandad Nahavandipoor Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @vandadnp

Feb 9
#Tips on how to up your #LinkedIn game
πŸ§΅πŸ‘‡πŸ»
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/
Read 9 tweets
Feb 6
Confused about #StatefulWidget and #StatelessWidget in #Flutter? In this thread I'll explain the most basic concepts.
πŸ§΅πŸ‘‡πŸ»
* 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/
Read 13 tweets
Feb 5
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
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(