Mixin is one of the features of Object-oriented features for code reusable and DRY principles.

Mixin is a reusable class in multiple classes in object hierarchies.

It adds new functionalities to a class with mixin.

It contains two parts creating and adding Mixin to a class

  • Define Mixin class
  • use Mixin class in a class

How to create a mixin and reuse class in Dart?

use the mixin keyword To create a mixin class This class does not define the constructor

Syntax:

Mixin classname{
    // instance variables
    // methods
}

to reuse the mixin, the with keyword is used.

class newClass with mixinclassname implements interface{}
class newClass1  extends a class with mixinclassname{}

with keyword is used with the below rules

  • with used before implementing keyword
  • with used after extends keyword
  • Can use multiple mixins separated by a comma.

Here is an example usage of mixin in dart

void main() {
  var circle = new Circle();
  print(circle.area);
  print(circle.display);
  var rectangle = new Rectangle();
  print(rectangle.area);
  print(rectangle.volume);

  print(rectangle.display);
}

class Shape {
  void display() => print('Shape');
}

class Circle with Area implements Shape {
  void display() => print('Circle');
}

class Rectangle extends Shape with Area, Volume {
  void display() => print('Rectangle');
}

mixin Area {
  void area() => print('Area');
}

mixin Volume {
  void volume() => print('volume');
}

Output:

Closure 'area$0' of Instance of 'Circle'
Closure 'display$0' of Instance of 'Circle'
Closure 'area$0' of Instance of 'Rectangle'
Closure 'volume$0' of Instance of 'Rectangle'
Closure 'display$0' of Instance of 'Rectangle'

Mixin errors

Following are errors with the usage of mixin in dart

  • Error: The with clause must be before the implements clause. if you are using the with keyword combined implements keyword, You should use the with keyword before the implements keyword The following code throws an error The with clause must be before the implements clause.
class Circle implements Shape with Area{
    void display() => print('Circle');

}

Here is Valid usage with the implemented code

class Circle with Area implements Shape {
    void display() => print('Circle');

}
  • Error: The extends clause must be before the with clause.

if you are using the with keyword combined with the extended keyword, You should use the with keyword after the extended keyword.

The following code throws an error Error: The extends clause must be before the with clause

class Rectangle with Area extends Shape {
    void display() => print('Rectange');

}

Here is Valid usage with extended code

class Rectangle  extends Shape with Area {
    void display() => print('Rectange');

}

Dart Mixins Constructor

A constructor can not be used in the mixin, It throws an error Error: Mixins can’t declare constructors.

mixin Area {
  Area(){

  }
  void area() => print('Area');
}

if you use a mixin with a constructor with the keyword as seen below, It throws Error: Can’t use ‘Area’ as a mixin because it has constructors.

class Circle with Area  {
  void display() => print('Circle');
}