Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixins in dart 2.1 - "on" keyword

Tags:

dart

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"?

like image 823
Chris G. Avatar asked Jan 04 '19 08:01

Chris G.


People also ask

How do you use mixins in Dart?

We make use of the with keyword followed by one or more mixins names. Mixins can be used in two ways, the first case is when we want to make use of class code in such a way that the class doesn't have any constructor and the object of the class is extended. In such a case, we use the with keyword.

What is the difference between a mixin and inheritance in Dart?

Mixins is not a way to get multiple inheritance in the classical sense. Mixins is a way to abstract and reuse a family of operations and state. It is similar to the reuse you get from extending a class, but it is compatible with single-inheritance because it is linear.

How do I add mixin to flutter?

To implement a mixin , create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class . In this way, your Car can run , but cannot be ' handleControlled '.

How do you define mixin flutter?

Mixins are a way of reusing a class's code in different class hierarchies. For example, you might have a class called Employee which has methods like clockIn . The code in those classes may be useful for both Bartender and Nurse .


2 Answers

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

like image 77
Günter Zöchbauer Avatar answered Nov 02 '22 02:11

Günter Zöchbauer


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

like image 40
Simeon Avatar answered Nov 02 '22 02:11

Simeon