Mixin in dart

Mixin in dart

Flutter is becoming the most popular language now for mobile development. When developers write code, they encounter the keyword 'with'. Now the question arises: what does that keyword mean in Dart programming language, and what is the use of it?

Let's discuss it. Mixin is a kind of inheritance that which promote the reuse of code, With the example of Students class , we can understand it in more detail.

class Students {
  
  void name(){}
  void roll(){}
  void subjects(){}
}        

Now to reuse the code, we can break it into smaller parts with the help of Mixin.

class Students  with Name,Roll{

  void subjects(){}
}

mixin Name {
  void name(){}

}
mixin Roll {
  void roll(){}

}        

This is how we can reuse code with the help of Mixin.


Why Mixin if we can do the same thing with the help of extends and implements?

extends and implments can use with inheritence but they have some limitations.

  1. extends allow to inherit only one class,
  2. implements allow to implements many interface but you have to define all the method which is already define in the interface.

To overcome above two implementation , we uses mixin in dart. I hope you understand the basics. I hope that will satisfy your curiosity about, with and implements or extends.





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

社区洞察

其他会员也浏览了