Mixins in Dart - Understand Dart & Flutter Fundamentals (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Worth mentioning about keyword "on" when working with mixins - mixins with super calls. There's literally nothing documented about this feature, but it's really useful. I learned about it from framework source code. Would be nice to know about that few months ago. https://github.com/dart-lang/language/issues/7

👍︎︎ 6 👤︎︎ u/yyyt 📅︎︎ Jul 22 2019 🗫︎ replies

Very good explanation! Thanks!

👍︎︎ 3 👤︎︎ u/ArchCatLinux 📅︎︎ Jul 21 2019 🗫︎ replies

Subscribed!

👍︎︎ 2 👤︎︎ u/SeveralResearcher 📅︎︎ Jul 22 2019 🗫︎ replies

I really liked that the video provided the written tutorial too. I like to read through first and if I still don't get it, I watch the video alongside.

👍︎︎ 2 👤︎︎ u/maan_ster 📅︎︎ Jul 24 2019 🗫︎ replies
Captions
in a classic object-oriented programming you have regular classes abstract classes and interfaces of course dart has its own take on interfaces but that's for another tutorial somewhere in the shadows though is lurking another kind of an animal mix-ins what are they for and when should you use them let's find out [Music] whoa welcome to resew colder where you are getting prepared for real app development by building better faster more stable apps so subscribe and hit the bell if you want to grow your coding skills in this tutorial just imagine that you are building a wildlife simulation app which needs to have at first a mosquito class so you first go ahead and create a mosquito class which will be only a simple class mosquito and it will have three functions actually any mosquitoes can crawl flutter their wings and they can also bus annoyingly so your mosquito has three functions everything is nice and good of course this is currently without make since we're going to get to why mixings are so useful later on when we run into some limitations with our wildlife simulation apps classes and also be sure to check out the written tutorial from the link in the description where you can also find all of the code written in this video and go through this lesson at your own pace alright so these three methods of a mosquito are awesome but then you decide Oh mosquito has some kind of a routine which a mosquito does so you're gonna create another new method called do mosquito thing which will just trigger every single thing by crawling fluttering and buzzing and then the skier will suck blood so you have mosquito class everything is nice and dandy but then as an experienced and foreseeing developer you deem that it's correct to extract the things mosquitoes have in common with other similar insects into abstract classes because if you just look at this crawling is common among every insect that there is and then for example other airborne insect like flies can also foster their wings and they can also bus so let's just extract these methods this functionality of a mosquito in two separate classes it's probably good to extract it into an abstract class so insect and this will halt crawling because not every insect has wings actually because some of the insects have lost their wings over time so in our abstract class we'll be airborne insect and this airborne insect will of course can flutter its wings and also bus while it's fluttering away and now all that's needed is that airborne insect actually extends regular insect and then also that the mosquito will extend airborne insect and now everything works again properly you pat yourself on the back that you do not have any code duplication in your wildlife simulation app that's until you realize that you also wanna have a swallow class which is a bird because something has to eat all of the mosquitoes after all and again and there are some actions which all birds have in common so you're not even going to create the swallow class initially you're first going to start off with the abstract bird class alright so let's do just that we're gonna create abstract bird what can birds do for example they can chirp and it's just going to print out chirp chirp but then it strikes you as you're creating your app that birds can also flutter their wings because birds are winged creatures but you cannot just extract the flutter method out of the airborne insect into another new class for example called fluttering why is that not possible well while birth could extend the hypothetical new class which would just contain fluttering or flutter method actually this would not be possible with airborne insect airborne insect cannot extend another class because it already extends insect and thankfully dart doesn't support multiple inheritance which is actually a good thing and if you want to learn more about multiple inheritance a link is available in the written tutorial on reso culture comm to which you can get from the link in the video description so because airborne insect can not simply extend another class we need to duplicate the functionality inside the bird class so there is code duplication and that is just no good but at least now the swallow class creation is possible because you can have a swallow in your wildlife simulation app but the problem is that while doing that you've also duplicated code this is the reason why mixings are so useful but first let's just run through a quick definition from the dart documentation it says that a mixin is a way of reusing a class it's code in multiple class here keys that sentence simply said means that mix-ins allow you to plug in blocks of code without needing to create subclasses it's literally as if you just took that code and plugged it pasted it into a class declaring a mixin is also very simple it's very similar to how you would declare a class so let's do just that let's go up here cut out the flutter method and above insect let's just create a mixin called fluttering which will have the void flutter method and now this mixing can be mixed into classes where regular or even abstract classes using the width keyword in this case it's probably good to mix it into the abstract classes so or an insect will be with fluttering and also in addition to airborne insect the bird will be mixed in with fluttering so mixings do not have the same problem as classes do or actually it's a blessing when it comes to classes because classes can be cast to another classes for example if you take the mosquito a mosquito can be cast into airborne insect and also into an insect because it's inheritance hierarchy however inheritance hierarchies just do not apply with mix-ins you cannot cast an insect or airborne insect into fluttering because it's not a class it's just a mix and you cannot cast anything into being a mixin so that's precisely what's so cool about mix-ins and also you can have multiple mix-ins apply to a single class so for example if we create fluttering to mixing we can have with fluttering and with fathering - that's entirely possible but in this wild life simulation apps case there is not gonna be like to make sense on a single class so we can just delete this photo ring - this was just for the sake of demonstration purposes the code right now is awesome it doesn't have any duplication but sometimes you want to prevent a mixing from being used with any kind of a class instead you want to limit a mixin to be used only on a certain class or its subclass for example you could create another mixin which would be limited only to birds because as you are doing your research on wildlife you realize that there are other birds like sparrows and blue jays and unlike swallows most of the other birds constantly pick something from the ground you know seed here earthworm there and so on so you want to add a pack method but adding a peck method directly into the bird class is not possible because not all birds pack for example as far as I'm concerned a swallow does not anything so because you wanna have Blue Jays and also sparrows you don't wanna just put the peg method directly inside these two classes so we're gonna create another mixin a mixin called pecking which will have a void peck which will just say pecking all right so now you go ahead and create two more classes which will be sparrows and blue jays which have empty class bodies because they just get all of their functionality either from the parent class or from the pecking mixin so it's cool right having the pecking mixin is much better than code duplication but even overlooking the fact that now you can use the pecking mixin even on insect classes which doesn't entirely make sense because insects just do not pack but now this is entirely possible which should not be possible because it's stupid insect cannot be pecking but apart from that if we overlook even this fact there is a much bigger issue at play with this pecking mixin that's because once you start researching the behavior of birds in their natural habitat you come to a conclusion that after pecking something from the ground birds just start chirping out of fear happiness and you already have a chirp method in the bird class so you'd really like to reuse the functionality of the chirp method because while all birds can chirp not all birds can Peck so you do not want to move it into the tacking mixin right and of course not all birds can sure because for example Eagles do not sure but let's just uh keep this simple here right so what you want to have here is something like chirp after packing but this is not possible because of course dart does not know whether or not this pecking mixing will be used only on bird classes so to fix this just tell Darth to restrict the usage of pecking Mixon only to birth classes and by doing that by providing the on operator and then bird now this Becky mixing can be only used on birds and their subclasses of course and because it can only be used on birds and birds have a chirp method dart figures out oh you can call chirp from the pecking mixin too because it's guaranteed to be available so if we now go ahead and try it out in main de Dart after importing all the flying things we just instantiate a mosquito and let it do its thing then a swallow will do its thing and then a sparrow will simply pick something from the ground so let's try it out let's run it in the console and as expected mosquitoes do their thing by crawling fluttering and buzzing annoyingly and then they suck blood then swallows just chirp flutter and then they eat that freaking mosquito and finally sparrows start taking something from the ground and out of your research you know they they should also chirp and surely sparrows and also blue jays will chirp so let's just try blue jay here they will chirp after pecking something from the ground this whole tutorial is available for you in the written form from reso coder comm so go there by clicking on the link in the video description and you can learn at your own pace if you don't want to miss the more tutorials like this definitely subscribe to this channel and also join the notification squad by hitting the bell button to make sure you grow your dart and flower skills because here on resew coder I am determined to provide you with the best app development tutorials and resources out there if this video helps you with understanding mixings indoors definitely give it a like and also share it with our developers who will surely benefit from it follow me on Instagram I go under the name reso coder everywhere leave a comment if you have anything to say and it's or questions and see you in the next video [Music]
Info
Channel: Reso Coder
Views: 35,897
Rating: undefined out of 5
Keywords: resocoder, tutorial, programming, code, programming tutorial, dart tutorial, dart mixin, dart mixin vs interface, flutter mixins, dart programming, dart 2, flutter tutorial, dart tutorial for beginners, dart tutorial for flutter, mixin dart, flutter dart, flutter dart tutorial, mixin vs class dart, mixin vs extend
Id: pJHTyhzt9JU
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Sun Jul 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.