Mixins in Dart: How to use it

This Blog Explains the Dire functionality of Mixins in Dart

Shivanchal Pandey
Jul 8 · 4 min read
Image for post
Mixins In Dart

There are different object-oriented programming (OOP) languages and Dart is one of them, basically, keeping code clean and dry is one of the characteristics of OOP. In OOP you have regular classes, abstract classes, and interfaces but in the dart, there is another hidden feature which is called mixins and this gives you that wow! which sometimes every developer looks for but the question is how?

Basically, the concept of mixins comes into effect when you want to inherit multiple classes at the same time but as you know dart does not allow multiple inheritances (which is a good thing ) and there you stand with two options either create a duplicate method or create mixins, wait….mixins?🤔

What are mixins?

Mixins are simple classes that are instantiated by using mixin to avoid multiple class hierarchies. if we go by definitions there are some quotes here

A mixin declaration introduces a mixin and an interface, but not a class. The mixin introduced by a mixin declaration contains all the non-static members declared by the mixin, just as the mixin derived from a class declaration currently does.


Mixins are a way of reusing a class’s code in multiple class hierarchies.

dartlang.org

Now you might get the sense behind the mixins. So let's understand it by example

Problem:

To sort out this we will understand the concept of mixin by the real-world example so you can easily understand it.

Suppose we have two animal classes where we have different kinds of animals some of them have some same and some different qualities, for example, there are two classes — alligator and crocodile.

Both of them have similarities in every aspect.

Both of them also come into the aquatic animal category but all animals who live in water don't have the same behavior so here we could create an abstract class which would have the same sort things which mostly aquatic animals have.

Now if we mention any other aquatic animal that has some other behaviors then ??? should we create another class of different behavior ???

No, you don't need because if that animal has some behaviors that reptile class consists and some which it does not then we have only the option to create a new class for that specific animal which will also extend reptile class.

You can do this but as you know flutter does not allow multiple inheritances then mixin comes into effect,

for example, see the code below there is class fish which have qualities like bit and swim but does not have crawl so if it extends that reptile class it would find some feature that it does not require then either fish class should create its own swim and bite method but it is code duplication so we would create Swim, Bite and Crawl as mixin classes.

In the above example as you can that using mixin classes does not require any special specifications you can inherit by using with keyword. You can inherit mulitple mixin classes.

But what if you want to limit the use of a particular mixin class —

Limiting the use of Mixins

Suppose if you want to prevent your mixin from being used on any class or you want to make you mixin class usable only for some class or its subclass it is basically you rely on some specific methods and field being already present.

For this, we only need to do only use “on” keyword.

So here in the example, as you can see that we have used on keyword to tell that it will only be used on reptile class and it will give access to its all functionality like Swim, Crawl and Bite mixins.

Conclusion :

So with this example, you can get the sense behind the mixins, it actually very useful in avoiding the code duplication without inheritance. Mixins can also be limited to work with any specific class and it makes it a powerful feature in the app development.

If this article has helped you a bit and found interesting please clap!👏

Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.

Image for post

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high…

Shivanchal Pandey

Written by

Connect with me here…www.linkedin.com/in/shivanchal-pandey-49755a179

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high quality. We’ve adopted Design First attitude which helps us deliver applications of highest quality.

Shivanchal Pandey

Written by

Connect with me here…www.linkedin.com/in/shivanchal-pandey-49755a179

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high quality. We’ve adopted Design First attitude which helps us deliver applications of highest quality.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store