9

From: https://medium.com/dartlang/announcing-dart-2-1-improved-performance-usability-9f55fca6f31a

Under Mixins:

mixin SomeClass<T extends SomeOtherClass>
on State<T>
implements ThirdClass

What is "on"?

Chris G.
  • 23,930
  • 48
  • 177
  • 302

2 Answers2

11

This mixin can only be applied to classes that extend or implement State<T> which is effectively the state of a stateful widget.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

Figuratively speaking on is the extends for mixins.

mixin A on B

is like

class A extends B

To choose which one to use is like to choose between composition or inheritance. More on that composition vs inheritance thing.

What is the difference?

class ExtraPowers{}

class ClassPowers extends ExtraPowers{}
mixin MixinPowers on ExtraPowers{}

Let's say our main character is class "C" and we want to give it some extra capabilities and powers. We can do that in two ways:

// e.g. inheritance
class C extends ClassPowers{}

or

// e.g. composition
class C with ExtraPowers, MixinPowers{}

So if we choose to extend we may need to comply to some requirements. And we can do that by passing arguments to super.

If we choose to gain powers by using with can't use super to pass requirements. The requirements are satisfied by having the same extra powers as our mixin (nothing is left hidden e.g composition) and that on keyword tells us what extra powers our mixin has. So in order to get powers from MixinPowers we first must get ExtraPowers.

class C with ExtraPowers, MixinPowers{}

In conclusion on just like extends gives access to members of another object but on is for mixins extends for classes. The difference is when you use mixin you must implement the objects after the on keyword.

More info

Simeon
  • 3,347
  • 1
  • 12
  • 15