Mixin in Dart

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Dart

In Dart, a mixin is a way to reuse a class's code in multiple class hierarchies.

A mixin adds functionality to a class without creating a subclass.

To use a mixin in Dart, you first have to define a mixin using the mixin keyword, followed by the mixin name and the code you want to include:

mixin MyMixin {
  // code you want to include
}

Then, you can use the with keyword to use the mixin in a class:

class MyClass with MyMixin {
  // class code
}

Here is a more detailed example that demonstrates how to use a mixin in Dart:

mixin CanFly {
  void fly() {
    print('I can fly!');
  }
}

class Bird {
String name;

Bird(this.name);
}

class Eagle extends Bird with CanFly {
Eagle(String name) : super(name);
}

void main() {
var eagle = Eagle('Golden eagle');
eagle.fly(); // Output: I can fly!
}

In this example, we defined a CanFly mixin that adds a fly() method.

We then created a Bird class and an Eagle subclass that extends from Bird and includes the CanFly mixin using the with keyword.

Finally, we created an instance of the Eagle class and called the fly() method to demonstrate that the CanFly mixin was successfully included in the class.