Menu
Coddy logo textTech

on Keyword in Mixins

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 53 of 110.

Sometimes a mixin needs to use methods or properties from the class it's mixed into. The on keyword lets you restrict a mixin to only be used with classes that extend a specific type.

class Animal {
  String name;
  Animal(this.name);
  
  void breathe() => print('$name is breathing');
}

mixin Swimming on Animal {
  void swim() {
    print('$name is swimming');  // Can access 'name' from Animal
    breathe();  // Can call Animal's methods
  }
}

class Fish extends Animal with Swimming {
  Fish(String name) : super(name);
}

The on Animal declaration means this mixin can only be applied to classes that extend Animal. This guarantees the mixin has access to Animal's members. Without on, the mixin wouldn't know that name or breathe() exist.

If you try to use a restricted mixin on an incompatible class, Dart gives you a compile-time error:

class Robot with Swimming {}  // Error! Robot doesn't extend Animal

This constraint is useful when your mixin's functionality depends on specific capabilities from a base class. It creates a safer contract - the mixin promises certain behavior, but only when it has the foundation it needs to work correctly.

challenge icon

Challenge

Easy

Let's build a vehicle system that demonstrates how the on keyword restricts mixins to work only with specific base classes. You'll create a mixin that adds turbo boost functionality, but it can only be used by classes that extend a Vehicle base class.

You'll organize your code into two files:

  • vehicles.dart: Define your base class, restricted mixin, and vehicle types here:
    • A Vehicle class with a String model property and an int speed property. Include a constructor that takes both values. Add a method accelerate() that prints [model] accelerating to [speed] km/h
    • A TurboBoost mixin restricted to Vehicle using the on keyword. This mixin should have a method activateTurbo() that prints [model] TURBO ACTIVATED! and then calls accelerate() (both accessible because of the on Vehicle constraint)
    • A SportsCar class that extends Vehicle and uses the TurboBoost mixin. Add a String color property. The constructor should take model, speed, and color. Include a method showOff() that prints [color] [model] ready to race!
  • main.dart: Import your vehicles file and demonstrate how the restricted mixin works:
    • Create a SportsCar with model 'Ferrari', speed 320, and color 'Red'
    • Call showOff()
    • Call activateTurbo()

The TurboBoost mixin can access model and call accelerate() because the on Vehicle declaration guarantees it will only be mixed into classes that extend Vehicle. This creates a safe contract where the mixin knows exactly what capabilities are available.

Expected output:

Red Ferrari ready to race!
Ferrari TURBO ACTIVATED!
Ferrari accelerating to 320 km/h

Cheat sheet

The on keyword restricts a mixin to only be used with classes that extend a specific type. This allows the mixin to safely access methods and properties from the base class.

class Animal {
  String name;
  Animal(this.name);
  
  void breathe() => print('$name is breathing');
}

mixin Swimming on Animal {
  void swim() {
    print('$name is swimming');  // Can access 'name' from Animal
    breathe();  // Can call Animal's methods
  }
}

class Fish extends Animal with Swimming {
  Fish(String name) : super(name);
}

The on Animal declaration guarantees the mixin has access to Animal's members. Without it, the mixin wouldn't know that name or breathe() exist.

Attempting to use a restricted mixin on an incompatible class results in a compile-time error:

class Robot with Swimming {}  // Error! Robot doesn't extend Animal

This constraint is useful when your mixin's functionality depends on specific capabilities from a base class, creating a safer contract between the mixin and the classes that use it.

Try it yourself

import 'vehicles.dart';

void main() {
  // TODO: Create a SportsCar with model 'Ferrari', speed 320, and color 'Red'
  
  // TODO: Call showOff()
  
  // TODO: Call activateTurbo()
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming