Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last video we have written a test to check that job done from map returns a job with the correct
properties.
However when we run this test we got a failure with this error message and the problem here is that
the compiler doesn't know how to compare instances of type job because we haven't provided any quality
operator for our job class.
And because this is a custom class that we have created then we need to override two things in the base
object class called the hash code and the equality operator and these look like this.
We have a hash code and we have an equality operator.
So what are these things exactly.
And how do we use them.
Well the hash code is an integer representation of an object and we can open the documentation to learn
more about it.
And the useful thing to know here is that hash code must be the same for objects that are equal to each
other according to the operator equal.
And the hash code of an object should only change if the object changes in a way that affects equality.
OK.
So this explanation gives us a vague idea about hash code but how should it be implemented.
Well let's get back to our job file and in here we are going to use a special function called hash values
like this and we need to import dark UI to make use of this and to this function we need to pass all
the properties that we have in our job class.
So in this case it's I.D. and name and rate but our like this.
And just to be clear this is the way that hash code is implemented in the classes inside the flatter
SDK.
So I recommend that you use the same convention for your own classes.
Next we need to implement the equality operator.
And what I'm going to do now is to write some code and then I'm going to explain it.
So here I can type.
If and then identical.
And then this with other than return true.
And then if runtime type is not equal to other dot runtime type that return false.
And finally I mean to type final job other job equals other and then return by the equals other job
dot idea and name equals other job.
The name and rate for our equals other job.
Right.
Our like this.
And these is the way that the equality operator is implemented for classes in the the decay.
So the first condition is to check if these two references are in fact the same object in memory.
And in this case we can return true.
And the second track is to verify if the other object is of the same type as the current one.
And this is necessary because the equity the operator could be used to combat objects of different types.
And if this is the case then we return false.
And if we get to this line it means that the other object is of type job so we can assign it to a new
variable of type job.
And finally we return true if all the properties match.
So this is the correct way of implementing the equality operator.
And in general it is good practice to define the hash code and the equality operator for our own classes
because this means that we can compare them directly and now that we have done this.
We can run our test once again and this time we got a success.
And just to be clear what happens when we call expect is that the equality operator is called between
the left and the right hand side arguments and this works because we have implemented the equality operator
for the job class.
Before we move on I want to mention that adding the hash code and the equality operator for our classes
can be a bit tedious because this is a bit of boilerplate code that we need to add for each class.
And on top of that if we add or remove properties from our classes then we have to remember to update
the hash code and the equality operator and especially if we have a lot of model classes that this can
become a lot of work that is error prone.
So one alternative approach to this is to use code generation tools that automate the process of creating
and updating model classes.
However we will not go where coding generation in this course and you can research this topic yourself
if you're interested.
OK so let's continue on the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.