What are mixins in Dart

What are mixins in dart :

Dart 2.1 introduced one new concept called mixins. In simple words, this is a class that contains methods that can be used by other classes. So, don’t we have already inheritance ? We can extend one class, right ? We can have one abstract class and extend it. So, why mixins ?

Life before mixins :

Suppose we have one program to create different objects of different birds and fishes. We are considering only three properties for birds : fly, walk, eat and two properties for fishes : swim and eat.

Ok, our base properties are fixed. So, we will create two abstract classes with these properties :

abstract class Bird{
  void fly();
  void walk();
  void eat();
}

abstract class Fish{
  void swim();
  void eat();
}

We have one similar method eat on both of these abstract classes. We can move it to a different class to make the code more reusable :

abstract class Bird{
  void fly();
  void walk();
}

abstract class CanSwim{
  void swim();
}

abstract class CanEat{
  void eat();
}

Looks nice. But wait. We can’t extend more than one class, right ? How can we create a bird that can fly, walk, swim and eat ? This is the problem with abstract class if you want to write your code in a more reusable way and this is where mixins come to the picture.

How to use mixins :

mixins are classes that extend objects. Yes, the above abstract classes are mixins. with a keyword is used with a class to use a mixin. We can use one or more mixins. Similar to extends, we need to override all methods if we are using it.

Example of class using as mixins :

abstract class Bird {
  void fly();
  void walk();
}

abstract class CanSwim {
  void swim();
}

abstract class CanEat {
  void eat();
}

class Duck with Bird, CanSwim, CanEat {
  
  void eat() {
    // TODO: implement eat
  }

  
  void fly() {
    // TODO: implement fly
  }

  
  void swim() {
    // TODO: implement swim
  }

  
  void walk() {
    // TODO: implement walk
  }
}

Here, we are using Bird, CanSwim and CanEat abstract classes as mixins. As these are abstract classes, we must override all of these methods. We can also use other normal classes as mixins and overriding is optional for those class methods.

Example of mixins :

We can also use mixin keyword to define a mixin :

mixin Bird {
  String birdName;

  void fly(){
    print("flying..");
  }
  void walk();
}

Similar to the above example, you can use mixin with ‘with’ keyword. If you don’t want to use mixins as a normal class, you can use mixin.

mixin and ‘on’ :

We can use on keyword to specify one mixin that can be used only on a specific class. For example :

mixin CanFly on Bird {
  void fly() {
    print("flying..");
  }
}

class Bird {
  void walk() {
    print("walking...");
  }
}

class Parrot extends Bird with CanFly {}

Dart mixin

Here, we have one mixin CanFly that can be used only with class Bird class. We can’t directly use it with any other class. It will throw one compile time error.

That’s all on mixin. Try to go through the examples I have shown above and drop one comment below if you have any questions.

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️