← → or space · progress saves for Continue on the roadmap
Goal
Reuse behavior across unrelated classes without forcing a single superclass tree.
Step 1 - Basic mixin
mixin Timestamped {
DateTime get stampedAt => DateTime.now();
}
class Event with Timestamped {
final String name;
Event(this.name);
}
void main() {
Event e = Event('login');
print('${e.name} at ${e.stampedAt}');
}Step 2 - Mixin with dependency on class
class Model {
String id = 'x';
}
mixin Pretty on Model {
String describe() => 'Model($id)';
}
class User extends Model with Pretty {}
void main() {
User u = User();
print(u.describe());
}on ModelmeansPrettymay only be applied to types that areModel(or subclass), soidis safe to use.
Step 3 - Order: extends then with
class Base {}
mixin M1 {}
mixin M2 {}
class C extends Base with M1, M2 {}Good habit
- Prefer composition for large state; mixins for small cross-cutting slices (logging, serialization hooks, equality helpers).
Practice tasks
- Add a
mixin Identifiedthat requiresString get idviaonand implementsbool sameAs(Identified other) => id == other.id. - Apply two mixins to one class and call methods from both.