ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

Equality in Dart | Decoding Flutter

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2022
  • Learn about different possible definitions for "equality", how Dart operates by default, and how you can alter that behavior to simplify your Dart and Flutter apps.
    Equality DartPad → goo.gle/3T10k0s
    Equatable package → goo.gle/3Elgiyo
    Watch more Decoding Flutter episodes → goo.gle/Decodin...
    Don’t miss an episode, subscribe to Flutter → goo.gle/FlutterYT
    #DecodingFlutter #Flutter #Developer

ความคิดเห็น • 83

  • @julien-scholz
    @julien-scholz ปีที่แล้ว +33

    This video contains wrong information: You should NOT use hash code in the equality operator. Two objects that are equal should have the same hash code, but two objects that are not equal can also share the same hash code. Mathematically, this is due to the pigeon hole principle. The Equatable package also does not use hash code in the equality function.
    Hash codes exist to speed up hash maps and other data structures. Finding an object in a map is much easier if you can boil it down to a simple integer value.

    • @fitness7065
      @fitness7065 ปีที่แล้ว +11

      This is correct, overriding equality with hash code only can result in some very very serious bugs, especially data corruption. Please get this video reviewed by any SDE at Google and everyone will tell you the same thing, that this breaks the equality contract and is very bad. This video should be pulled for spouting this dangerous misinformation. Otherwise it would have been a great video.

    • @Aragorn450
      @Aragorn450 ปีที่แล้ว +1

      @@fitness7065 Edited second part below:
      This is not overriding anything in the way equality works, it's just overriding the hash code for a Class that extends the Equatable package/class. So normal equality will be just fine and won't be broken.
      I previously stated that Dart uses the hashCode to compare equality of objects but that was incorrect. In the end, it natively uses the pointer address to compare two objects. Also, @Julien Scholz's edited statement that the Equatable package does not use the hashCode to do a equality check is correct. It does update the hashCode to be the same between two instances of the same class that have the same values however, so hash tables and the like would consider the object already existing if a second instance is added that has the same values.
      Finally, while it is possible that there would be collisions, doing a comparison with the hashCode as demonstrated in the video wouldn't be as catastrophic as is being suggested. The first check is that the two objects are of the same type. So for a collision to happen, you would have to have two objects of the same type that have values that then collide, which is a pretty unlikely situation in the real world.

    • @fitness7065
      @fitness7065 ปีที่แล้ว

      @@Aragorn450 Hi Charlie, thanks for the reply! I'm referring to the override described at 3:37 in the video, and the explanation that begins a few seconds before that. That appears to be overriding the default equality and breaking the equality contract.

    • @craiglabenz9147
      @craiglabenz9147 ปีที่แล้ว +6

      Hey all, thanks for the feedback! I didn't intend for that to be stopping point; merely a building block toward understanding what Equatable does and why you'd want to use it. I'll be more careful with phrasing around intermediate steps in the future.

  • @bdmaxvalera
    @bdmaxvalera ปีที่แล้ว +8

    Equatable is a must use package for all flutter projects, it saves lot of time ❤️

  • @prasantkumar7693
    @prasantkumar7693 ปีที่แล้ว +33

    Very clean explanation by Flutter Team. I love it. Thank you.

    • @flutterdev
      @flutterdev  ปีที่แล้ว +3

      We always love hearing feedback from viewers like you! Glad to hear you enjoyed it. Thanks for the love 🙌

  • @hasansalim1868
    @hasansalim1868 ปีที่แล้ว +2

    Thanks Craig and the Flutter team.

  • @acavals8080
    @acavals8080 ปีที่แล้ว +15

    Not sure I'm gonna use it, but it's amazing to know we can override an operator 😂

    • @blablablabla29382
      @blablablabla29382 ปีที่แล้ว +7

      When using flutter_bloc it's a must have. It ensure that your state is well emitted and not removed because object seem the same.

    • @pedrolemoz
      @pedrolemoz ปีที่แล้ว +3

      You may use it in your unit tests. For example, if you wanna test if two instances of an object are equivalent, you can use the expect function of package test. It will use the equal operator, and by overriding the equal operator, we can verify if they are the "same" or not.

    • @Aragorn450
      @Aragorn450 ปีที่แล้ว +2

      To be clear, it's not exactly overriding the operator, it's just overriding how Flutter gets the value of the specific class you've defined that has that override enabled. For instance, doing something like this wouldn't allow you to make 3 == 4 return true. But you COULD make A(1) == A(2) return true with this...

    • @acavals8080
      @acavals8080 ปีที่แล้ว

      Thanks a lot for all the tips! I can't help falling in love with Flutter.

    • @daraxdray
      @daraxdray ปีที่แล้ว

      So what alternative do u use? Or you haven’t had an encounter with equalities?

  • @Bozacar
    @Bozacar ปีที่แล้ว +12

    What about hash collisions? If 2 objects have same hash code doesn't mean that they are the same

    • @laybunzz
      @laybunzz ปีที่แล้ว +3

      Yes, hash collisions should always mean that two objects are "the same". The idea here is that you get to define what "the same" means!

    • @gabrielterwesten3262
      @gabrielterwesten3262 ปีที่แล้ว +11

      @@laybunzz Thanks for all the great work, but substituting component wise comparison of fields in the equals operator with a single comparison of hash codes breaks the contract of the equals operator. Two objects that are equal must have the same hash code, but two objects which are not equal are allowed to have the same hash code, too.

    • @dongdigital
      @dongdigital ปีที่แล้ว +1

      @@gabrielterwesten3262 I don't think such substitution actually breaks the contract. Two equal objects (according to our new definition of equal) will always have the same hashcode. Non equal objects will never have the same hashcode, but that's not part of the contract. :)
      It can still be wrong though, since two objects that would not be equal by standard definition (comparing each field individually) might be accidentally detected as equal due to hash collisions.

    • @gabrielterwesten3262
      @gabrielterwesten3262 ปีที่แล้ว

      @@dongdigital The contract of the equality operator and the hashCode getter as specified in the API docs are not really optional. Collections rely on the correct implementation of those contracts. The type of equality specified there cannot be implemented with hash codes because of the collisions.

  • @Destiny_Ed
    @Destiny_Ed ปีที่แล้ว +3

    I love the Flutter T-shirt

  • @user-dy7eh8qq5c
    @user-dy7eh8qq5c ปีที่แล้ว +1

    Thanks Flutter team!

  • @aytunch
    @aytunch ปีที่แล้ว +2

    What about deep nested Maps and Lists? Would equatable understand 2 deep nested maps are same? Does it do some recursion under the hood in order to override its hashmap?

    • @wouterferemans5131
      @wouterferemans5131 ปีที่แล้ว +2

      Yes it does, with the help of the collections package, it checks if the properties in the props is a map, iterable or list and compares it with DeepCollectionEquality

  • @akaneritsuki
    @akaneritsuki ปีที่แล้ว +1

    equatable is nice package, though it'd be great if there's data class on dart

  • @VinayBaise
    @VinayBaise ปีที่แล้ว

    Also please decode bool vs Future, and what is the use use of Future when it can't be used to check condition

    • @prankcial4187
      @prankcial4187 ปีที่แล้ว

      Future is used because it provides a Boolean value in the future. Suppose you've to get data from some task but that task will take a few seconds or minutes to complete but it may block the main thread if we will wait for it. So to make the task asynchronous, we use await keyword and that's where Future value comes in.
      The task will complete asynchronously and then you'll get the value. You can further change the state of the widget to make changes in the screen using that awaited value.
      Hope this helps. :-)

    • @VinayBaise
      @VinayBaise ปีที่แล้ว

      @@prankcial4187 Bro thanks for replying, but my question is, when you get the asynchronous bool result it can't be used anywhere.
      The major use case of bool variable is to check whether the condition is true or false.
      Suppose I have written a function to check whether the device is connected to the internet or not, and it will ping to my server, and on successful connection it will return Future (true) on any socket exception (false).
      But the real problem is, you can't use the Future anywhere, neither in if else statement, nor you can assign it to other local bool variables.
      If you try to do so, the error will be:
      'Future ' is not a subtype of type bool
      The internet checking example I mentioned was not a big issue, you can anyhow solve that by assigning the value to a global bool variable. But in some scenarios it will become really difficult to overcome this issue

  • @ThEGeEeK
    @ThEGeEeK ปีที่แล้ว +1

    ❄️ Freezed package handles everything under the hood

  • @clever_dude
    @clever_dude ปีที่แล้ว +1

    I still prefer using "Command + n" -> "generate ==() and hashCode", it's way faster

  • @haseebkahn4811
    @haseebkahn4811 11 หลายเดือนก่อน

    What if i don't have final members in a class and i want to modify the objects' properties, what should i do?

  • @manishmg3994
    @manishmg3994 2 วันที่ผ่านมา

    why to check if other is of type A just modify your == operator with covariant

  • @AItaleOfficial
    @AItaleOfficial ปีที่แล้ว +1

    Excellent explanation.💯

  • @m31963
    @m31963 ปีที่แล้ว +1

    really really cool explanation !! keep it up.

  • @abdullahaqrabawy6897
    @abdullahaqrabawy6897 ปีที่แล้ว +4

    thank you very much
    i have one important question
    how did you animate the code like this :)
    is there a specific method or tool to do that?

    • @abdullahaqrabawy6897
      @abdullahaqrabawy6897 ปีที่แล้ว

      No answer 😭

    • @Onenrico
      @Onenrico ปีที่แล้ว +1

      @@abdullahaqrabawy6897 you could use microsoft powerpoint or other presentation software that support fade in

    • @craiglabenz9147
      @craiglabenz9147 ปีที่แล้ว +1

      @@abdullahaqrabawy6897 We use After Effects!

  • @jeffdon9257
    @jeffdon9257 ปีที่แล้ว

    very clean explanation.....
    can you show me how to use regex to validate email and password in dart?
    thank you

  • @dushes_botalov
    @dushes_botalov ปีที่แล้ว +2

    Thank u 😊

  • @codegambit2507
    @codegambit2507 ปีที่แล้ว

    This is so cool. But another issue u run into is if you want your class to extend another class. In Dart, a class can't extend more than one classes so it's impossible to extend Equattable and another class.
    A solution for this (if u use VSCode) is to install the Dart class generator extension. This would generate the equality code for u

    • @sahilkumar2953
      @sahilkumar2953 ปีที่แล้ว +3

      You have equatable mixin for that case.

    • @YayoArellano
      @YayoArellano ปีที่แล้ว +2

      Use equatable mixin. Avoid writing boilerplate code

    • @codegambit2507
      @codegambit2507 ปีที่แล้ว +1

      ​ @Yayo Arellano ​ @sahil kumar oh cool. It's been long I used the package. I didn't know about that. But I think i'll stick to the vs code extension. Rather than importing another dependency, It's easier and better in my opinion to use the extension to generate the code for me. "cmd + ." is all I need.

  • @mbsd0
    @mbsd0 ปีที่แล้ว

    I love the Decoding Flutter series! 💜
    On unrelated question though: What tool do you use to create these cute text animations? I mean the ones you use to show the code block changes in these videos.

    • @craiglabenz9147
      @craiglabenz9147 ปีที่แล้ว +2

      Thanks so much! As for the animations, we have a really talented team that uses After Effects to bring these videos to life :)

  • @alexandruterente9098
    @alexandruterente9098 ปีที่แล้ว +1

    Great vid!

    • @flutterdev
      @flutterdev  ปีที่แล้ว +1

      We're glad you found Craig's tutorial helpful, Alexandru! To help you on your Flutter journey, feel free to check out more Decoding Flutter episodes → goo.gle/DecodingFlutter
      Happy Fluttering 😁

    • @alexandruterente9098
      @alexandruterente9098 ปีที่แล้ว +1

      @@flutterdev I will thx!

  • @greglee7708
    @greglee7708 ปีที่แล้ว +1

    Great one, ty

  • @Pedro5antos_
    @Pedro5antos_ ปีที่แล้ว +1

    AWESOME!

    • @flutterdev
      @flutterdev  ปีที่แล้ว

      We couldn't agree more, Pedro! We have even more resources for our Flutter community. Try your hand at one of our amazing codelabs, and the best part? You can follow along at your own pace 🙌: goo.gle/flutter-codelabs

  • @texnikru
    @texnikru ปีที่แล้ว +1

    Very useful. Thanks!

    • @flutterdev
      @flutterdev  ปีที่แล้ว

      You've made a great point, Rudolf 🎯 Let us know how you'll be using this feature for your project or app!

  • @fredgotpub871
    @fredgotpub871 ปีที่แล้ว

    Thanks, I see the link between const constructor in Flutter and that Flutter doesn't rebuild those const widget: the default equality must be used.
    Though would it be possible to override equality for a subclass of widget to avoid rebuild even with non const constructor?

    • @rickycoates3003
      @rickycoates3003 ปีที่แล้ว

      Well it's really not, basically you should mostly compare the properties inside your logic rather then the class itself

  • @gaxkiller
    @gaxkiller ปีที่แล้ว +1

    Freeeeeeeeeezed

  • @sahil1307
    @sahil1307 ปีที่แล้ว

    Can someone comment down some good recourses ( including book , courses etc )to learn dart and flutter and how to develop a great app overall
    i have no prior experience in app development.
    i know about little bit about c and c++

    • @flutterdev
      @flutterdev  ปีที่แล้ว +1

      Getting started is tricky sometimes, we do offer many resources to help you get started however!
      Check out our Begin learning Flutter series right here on TH-cam: goo.gle/3CL1VlD
      If you prefer written guides, we also have a Get started guide on our resource site: goo.gle/3SRCSDe
      We can't wait to see what you create 🙌 🌠

    • @sahil1307
      @sahil1307 ปีที่แล้ว +1

      @@flutterdev Thanks so much for this 🤗

  • @jeffreycordovavargas3912
    @jeffreycordovavargas3912 ปีที่แล้ว

    Perú: Algun ejemplo de como leer codigo de barras mediante PDA o Terminal Android Sunmi L2

  • @NajeebAslan
    @NajeebAslan ปีที่แล้ว

    Great

  • @pablojaviergarcia4411
    @pablojaviergarcia4411 ปีที่แล้ว +1

    For once, the software is actually really useful

  • @indofiz1077
    @indofiz1077 ปีที่แล้ว

    Just like JS Brother

  • @RawPeds
    @RawPeds ปีที่แล้ว

    4:01 this is still bad: you have to repeat the field names inside the variable "props'".
    Need something cleaner.

    • @RawPeds
      @RawPeds ปีที่แล้ว

      Sorry for being blunt. I like dart, and i hope you can improve this feature in the future

  • @erperejildo
    @erperejildo ปีที่แล้ว

    After 3 years using Flutter I just realised the logo is a F.
    omg...

  • @marctan2247
    @marctan2247 ปีที่แล้ว

    good thing freezed exist!

  • @user-yp6ue8op7k
    @user-yp6ue8op7k ปีที่แล้ว

    Seriously??? Usin comparing hash codes while equals??? What’s about collisions?

  • @TimnewCNYN
    @TimnewCNYN ปีที่แล้ว

    🤯🤯🤯🤯
    Determine objects equality by comparing hash only, without considering hash collision.
    And the equatable library mentioned actually does not determine object equality by comparing hash code as explained in the video, instead it does compare each value of fields.
    I’m not being picky, but as a in-depth educational video, I would say this is an unforgivable error!
    And this kind of mistake should not be hard to be caught by doing a simple peer review.

  • @marcom.
    @marcom. ปีที่แล้ว +13

    It's odd to see that the newer language Dart even is a greater mess than Java when it comes to such basic concepts.

    • @dancovich
      @dancovich ปีที่แล้ว +7

      Why? Seems like many other languages, objects have their references compared unless you override the equality operator. What is a mess in this?

    • @marcom.
      @marcom. ปีที่แล้ว +2

      @@dancovich First thing: There is only the == operator left for equality, so how do you differentiate equality and identity like Java does with equals() and ==?
      Second thing: To make equality manageable they offer a library and you have to extend Equatable - this really is so '90s.

    • @dancovich
      @dancovich ปีที่แล้ว +6

      ​@@marcom. Dart has the `identical` function, part of the SDK, which will always compare references. Dart approach is that == will aways run whatever routine is writen for the operator and "identical" will always compare references. The reason == compares references by default is because the Object class uses "identical" in its == operator, so every class inherits this behavior by default.
      So, both Java and Dart have two ways of comparing, one is overridable and one is not. Only difference is that in Dart what is overridable is the behavior of the operator while Java what is overridable is the method. This isn't more or less confusing, it's just a different approach for the same issue that these languages don't have any operator for saying you're copying the value of an object to another and not just copying the reference.

    • @pedrolemoz
      @pedrolemoz ปีที่แล้ว +1

      ​@@marcom. I understand your point, but you don't need to use equatable if you don't want to. I personally used it in the past, but nowadays, I prefer using a VS Code extension called Dart Data Class Generator to override the == operator and hashCode. I want my entities to be apart of any external dependencies, even if its a great package like equatable or dartz, for example. Dart is not a mess like other languages. Dart gives you the basics of a programming language, and by doing so, it can be really predictable and safe to use.

    • @pedrolemoz
      @pedrolemoz ปีที่แล้ว +3

      For example, Dart don't have interfaces. Instead, you should use abstract classes. Dart don't support method overloading, which is SUPER common in Java, but instead, you should use factories to "overload" constructors and create another function to "overload" a method. In my humble opinion, this is much better. Methods and constructors have meaningful names, and this is great in terms of readability.

  • @5erTurbo
    @5erTurbo ปีที่แล้ว

    Isn't this a poor language design? If a library can generate that for you, compiler should also be able to do it?

  • @sarahfortunes6361
    @sarahfortunes6361 ปีที่แล้ว

    :)

  • @rohithgilla9492
    @rohithgilla9492 ปีที่แล้ว +1

    Thanks for freezed package 🫡

    • @flutterdev
      @flutterdev  ปีที่แล้ว

      You're truly welcome, Rohith. We're loving this cool package as well 🧊