Mixins

On occasion, the usual subclass and interface approach to classes is not quite right. One such case occurs so often that Dart supports it directly in the language: mixins. The purpose of mixins is to augment subclassing with additional behavior. This additional behavior is typically something that we will want to reuse in other classes as well.

Consider two simple classes, Person and Animal:

classes/mixins.dart
 
class​ Person {
 
String name;
 
Person(​this​.name);
 
}
 
 
class​ Animal {
 
String name;
 
Animal(​this​.name);
 
}

These two classes are unrelated to each other, though they do share a common name property.

During the course of working with these classes, we decide that we need subclasses of both. The Friend class will extend ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.