Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Mixins in Dart: How to use it

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.

https://gist.github.com/shivanchalaeologic/9881d1e8792f193cb1d6df634e21d990#file-alligator-dart

https://gist.github.com/shivanchalaeologic/881ec1febb4e443b32fb733b3cecf3c1#file-crocodile-dart

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 that would have the same sort of things that most aquatic animals have.

https://gist.github.com/shivanchalaeologic/f29606d320e7bd51c35b1e786d9a1e0b#file-animal-dart

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.

https://gist.github.com/shivanchalaeologic/7d46d908d727dd6f3f3d774565666973#file-animal-dart

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.

https://gist.github.com/shivanchalaeologic/8143db19617c9ee33c19081c7c59d717#file-mixindemo-dart

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!.

Leave comment

Your email address will not be published. Required fields are marked with *.