Mixin in Dart:

Mixin in Dart:

As the name suggest Mixin is use to mix it with some thing .In Flutter Mixin is way to reuse a code into multiple class hierarchies. Mixin Allows to use multiple class functionalities into a single class. Mixin are use to set a particular behavior on multiple classes without using inheritance. A mixin is applied to a class using with keyword and we can create a Mixin using mixin keyword.

Syntax to Create a Mixin:

We use mixin keyword to create it as below

mixin Logger
{           
void log(String message)
{
print("Log: $message")
}
}        

Syntax to use a Mixin:

We use with keyword to use the properties of mixin

class Sample with Logger
{
void logMessage()
{
log("This is from Sample class")
}
}

void main() 
{
var sample = Sample;
sample.logMessage();
}        

Output: This is from Sample class

Explanation: In the above example we created a mixin using mixin keyword class Sample will use it using with keyword and we create a instance of Sample class in main() method and used the code of Logger mixin.

Note : You can not create object of a mixin in a Dart that means mixin can not have constructors since constructors are the primary way to initialize a object in dart . A mixin is designed to provide only reusable properties and method to other classes.


on Keyword in mixin:

You can restrict a mixin to be used only by certain types using the on keyword.

In the below code Swimmer class can be used by the class which extends Animal class

mixin Swimmer  on Animal
{ 
void swim()
{
  print("Swimming");
}
}        
class Fish extends Animal with Swimmer
{
}        

Above code will work perfectly because class Fish extend Animal so it can work with swimmer.

class Crocodile with Swimmer
{}         

Above code will show error to mix Swimmer mixin with it because this class does not extends Animal class.

Conclusion:

Mixins are a powerful way to reuse code across multiple classes without relying on inheritance. They allow you to define shared methods and properties that can be combined with other classes.




Sandeep Verma

iOS Lead Developer || iOS || Swift || SwiftUl || Objective-C || Data Structure Enthusiast & Problem Solver || Team Management

3 个月

Very informative

要查看或添加评论,请登录

Arpan Bhatia的更多文章

  • Closures in Flutter

    Closures in Flutter

    A Closure is Function in Flutter that capture a value from its surroundings scope, even after scope exited. It is…

  • Higher Order Functions in Dart & Flutter:

    Higher Order Functions in Dart & Flutter:

    Higher Functions are the function that are used to make our code more concise, flexible and reusable. A higher order…

  • Extensions in Flutter

    Extensions in Flutter

    In Flutter extensions allowed to you to add some new functionality to existing class without modified their original…

  • Kotlin Flows and Channels

    Kotlin Flows and Channels

    In modern software development, asynchronous programming has become a crucial aspect of building responsive and…

    1 条评论
  • StateLess Widget in Flutter

    StateLess Widget in Flutter

    Stateless widget simply means the widget that will be return in this class will not contain any state and it will never…

社区洞察

其他会员也浏览了