Sub topic 13
Mixins and the `with` Clause
This chapter assumes basic Dart syntax, null safety, and familiarity with classes, methods, and inheritance. A mixin is a reusable group of methods and fields that a class can add without becoming a subclass of the mixin.
Why use a mixin?
Dart allows a class to extend only one superclass:
class Duck extends Animal {
// ...
}A mixin provides another way to share behavior. A class uses a mixin with the with clause:
class Duck extends Animal with Swimmer, Walker {
// ...
}The class still extends Animal, but it also receives the members declared by Swimmer and Walker.
A mixin usually represents an ability or feature, such as swimming, logging, caching, or validation. It does not usually represent an “is a” relationship.
flowchart TD
Animal --> Duck
Swimmer --> Duck
Walker --> Duck
Duck --> duckObject[Duck object]The superclass supplies the main identity. The mixins add reusable capabilities.
Declaring and applying mixins
Declare a mixin with the mixin keyword. A mixin can contain fields, methods, getters, and setters. It cannot have a generative constructor.
mixin Swimmer {
void swim() => print('Swimming');
}
mixin Walker {
void walk() => print('Walking');
}
class Animal {
final String name;
Animal(this.name);
String describe() => 'Animal $name';
}
class Duck extends Animal with Swimmer, Walker {
Duck(super.name);
}
void main() {
final duck = Duck('Donald');
print(duck.describe());
duck.swim();
duck.walk();
}The output is:
Animal Donald
Swimming
WalkingDuck inherits describe from Animal. It gets swim from Swimmer and walk from Walker.
A class can apply several mixins. The mixins are listed from left to right:
class Duck extends Animal with Swimmer, Walker {}The class can use all members that are visible from its superclass and applied mixins.
Mixin limitations and common errors
A mixin is not a standalone object factory. You cannot create an instance with Swimmer().
A mixin also cannot replace the superclass in an extends clause:
// Invalid:
// class Duck extends Swimmer {}Use with to apply it:
class Duck extends Animal with Swimmer {}If two mixins provide members with the same name, the later mixin in the with list takes precedence. Avoid unclear conflicts, or override the member in the class itself.
Use a mixin when behavior can be shared independently of one class hierarchy. If the behavior needs a particular constructor, identity, or independent stateful object, a regular class combined through composition may be a better choice.
| Design need | Common choice |
|---|---|
| Share an ability across unrelated classes | Mixin |
| Model a direct parent-child relationship | `extends` |
| Require a public contract | `implements` |
| Delegate work to a separate object | Composition |
Exercise
Write a Trackable mixin with a track() method, then apply it to a FileUpload class that extends Task.
Show answer
Declare mixin Trackable { void track() => print('Tracking'); }, then write class FileUpload extends Task with Trackable { FileUpload(super.name); }.