Updated 22 December 2023
In this blog, we are learning about how to use the mixin class in Dart and why to use this. From my time with Dart, one topic that I feel is a little tricky is mixins.
“A class that contains methods for use by other classes without having to be the parent class of those other classes is known as a mixin in object-oriented programming languages.”
Mixins are a way of reusing a class’s code in multiple class hierarchies. They provide an elegant way to reuse code from different classes that don’t quite fit into the same class hierarchy.
Mixin allows dart developers to use methods and properties that are not defined in the same class – without subclassing or extending the other class.
To put it another way, mixins are regular classes from which we can import variables or methods without having to extend the class. We may accomplish this in Dart by utilizing the “with“ keyword.
Read more about Flutter app development from mobikul.
To implement a mixin
, create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin
keyword instead of class
.
To use a mixin, use the with
keyword followed by one or more mixin names. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Vehicle { void run() { print( " I am a Car"); } } // defining mixin mixin MotorVehicle { void handleControlled() { print(" controlling car"); } } class MotorBike extends Vehicle with MotorVehicle { MotorBike motorBike= new MotorBike(); assert(MotorBike is Vehicle); // MotorBike is Vehicle = true assert(MotorBike is MotorVehicle); // MotorBike is MotorVehicle= true motorBike.handleControlled(); //we got the method without inheriting MotorVehicle } |
Class “MotorBike” extends Object with “MotorVehicleis” the same as class “MotorBike” with “MotorVehicle” in code.
Classes can now be used as mixins only if they extend objects alone. It is not possible to utilize classes that extend other classes as mixins.
However, given Dart is always changing and we already have an experimental feature that lets us use mixins to extend classes other than Object,
this might change in the future. The behavior of mixins may have changed slightly by the time you read this article.
Therefore, constantly pay attention to mixin specifications.
“Create a class that extends Object and declares no constructors in order to implement a mixin. Use the mixin keyword rather than class unless you want your mixin to function as a regular class.
Sometimes you might want to restrict the types that can use a mixin. You can restrict a mixin’s use by using the on
keyword to specify the required superclass. The on keyword describes which inheritance structure the mixin can be used on. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class MotorBike { void drivingBike(){ Print("Rider drive the bike..."); } } mixin MotorVehicle on MotorBike { void startEngine() { Print("Staring bike Engine... "); } } class SportsMotorBike with MotorVehicle { SportsMotorBike sportBike=new SportsMotorBike(); sportBike.drivingBike(); sportBike.startEngine(); } |
In the preceding code, you can extend the object in MotorVehicle class directly using the “on” keyword and the same class extends with SportsMotorbike using the “with” keyword.
Mixins are an excellent technique to reuse code unrestrictedly across several class hierarchies. I think there are a lot of things that mixins can be used for.
Thanks for reading.
Any feedback is welcome.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.