Dart Mixins
Dart Mixins
A mixin is a group of fields and methods that you can add to a class.
Mixins let you reuse the same code in many classes, even when the classes do not share a superclass.
You create a mixin with the mixin keyword.
You add it to a class with the with keyword:
Example
mixin Swimming {
void swim() {
print('Swimming in the water');
}
}
class Fish with Swimming {}
class Person with Swimming {
String name;
Person(this.name);
}
void main() {
var fish = Fish();
fish.swim();
var person = Person('Anna');
person.swim();
}
Try it Yourself »
Result:
Swimming in the water
Swimming in the water
Both Fish and Person now have the swim() method.
The code of the method is written only once, in the mixin.
Note: You cannot create an object from a mixin.
A mixin is only used to add code to classes.
Several Mixins
A class can only extend one class, but it can use many mixins.
Separate the mixin names with commas:
Example
mixin Walking {
void walk() {
print('Walking on land');
}
}
mixin Swimming {
void swim() {
print('Swimming in the water');
}
}
mixin Flying {
void fly() {
print('Flying in the sky');
}
}
class Duck with Walking, Swimming, Flying {}
class Penguin with Walking, Swimming {}
void main() {
print('Duck:');
var duck = Duck();
duck.walk();
duck.swim();
duck.fly();
print('Penguin:');
var penguin = Penguin();
penguin.walk();
penguin.swim();
}
Try it Yourself »
Result:
Duck:
Walking on land
Swimming in the water
Flying in the sky
Penguin:
Walking on land
Swimming in the water
A penguin cannot fly, so the Penguin class does not use the Flying mixin.
Mixins with extends
You can use extends and with together.
Write extends first.
A mixin can also have fields:
Example
mixin Counter {
int count = 0;
void increment() {
count++;
}
}
class Animal {
String name;
Animal(this.name);
}
class Dog extends Animal with Counter {
Dog(super.name);
void bark() {
increment();
print('$name says Woof (bark number $count)');
}
}
void main() {
var dog = Dog('Max');
dog.bark();
dog.bark();
dog.bark();
}
Try it Yourself »
Result:
Max says Woof (bark number 1)
Max says Woof (bark number 2)
Max says Woof (bark number 3)
The on Clause
Sometimes a mixin needs to use fields or methods from a class.
The on keyword says which class the mixin can be used with.
The mixin can then use the members of that class:
Example
class Musician {
String name;
Musician(this.name);
}
mixin Singer on Musician {
void sing() {
print('$name is singing');
}
}
class Pianist extends Musician with Singer {
Pianist(super.name);
}
void main() {
var pianist = Pianist('Elton');
pianist.sing();
}
Try it Yourself »
Result:
Elton is singing
The Singer mixin uses name, which comes from Musician.
If you use the mixin with a class that does not extend Musician, you get an error.
This is true even if that class also has a name field:
Example
class Musician {
String name;
Musician(this.name);
}
mixin Singer on Musician {
void sing() {
print('$name is singing');
}
}
class Robot {
String name = 'Robo';
}
class SingingRobot extends Robot with Singer {}
void main() {
SingingRobot().sing();
}
Try it Yourself »
Result:
main.dart:17:7: Error: 'Robot' doesn't implement 'Musician' so it can't be used with 'Singer'.
- 'Robot' is from 'main.dart'.
- 'Musician' is from 'main.dart'.
- 'Singer' is from 'main.dart'.
class SingingRobot extends Robot with Singer {}
^
Mixin Classes
A normal mixin cannot be used as a class, and a normal class cannot be used as a mixin.
If you need both, write mixin class:
Example
mixin class Logger {
void log(String message) {
print('LOG: $message');
}
}
class App with Logger {}
void main() {
var logger = Logger();
logger.log('Created as a class');
var app = App();
app.log('Used as a mixin');
}
Try it Yourself »
Result:
LOG: Created as a class
LOG: Used as a mixin
Tip: Use a mixin for a small skill that many different classes can have, like walking, logging or counting.
Use extends when one class really is a special kind of another class.