Mixin Use mixins to extend specific behaviors to classes

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

What are mixins?

A mixin is just like a class, it can have methods and properties, but it can’t be instantiated.

file_type_dartlang mixins.dart
mixin Strong {
  bool doesLift = true;

  void benchPress() {
    print('doing bench press...');
  }
}

mixin Fast {
  bool doesRun = true;

  void sprint() {
    print('running fast...');
  }
}

What are mixins used for?

Mixins are used to extend specific behaviors to classes with the with keyword. Certain Flutter libaries use mixins to extend shared behaviors to classes.

file_type_dartlang mixins.dart
class Human {}

class SuperHuman extends Human with Strong, Fast {}

Q&A Chat