Duplicate method behavior
The code, here as a Gist, will print e
. If I remove the override, i.e. remove output
from Baz
, it will print w
from Bar
.
This leads me to the conclusion that the method "priority" is own class
->
mixin
->
super class
.
If I add more mixins, e.g. like this:
mixin Zoo {
output() {
print('j');
}
}
class Baz extends Foo with Bar, Zoo {
// ...
Now, the output is j
. If I swap around Bar
and Zoo
:
class Baz extends Foo with Zoo, Bar {
// ...
Now, the outpt is w
again.
Consequently, I would define the priority like this: own class
->
last mixin
->
nth-last mixin
->
super class
.
Question
Is there any way for me to control this behavior, i.e. call the super call method even when a mixin
has a method with the same name?
Why
You might be askin why I would want to do this and not just rename the methods.
Well, in Flutter all State
's have a dispose
method and if I have a mixin
that has dispose
method as well, it will break the State
's functionality because the mixin
's dispose
method takes priority as illustrated above.
Additional notes
super.output
will call the mixin method as well, which is why that does not work. You can try adding the following constructor to Baz
:
Baz() {
super.output();
}
Even if this worked, it would not help as the dispose
method in the Flutter case is called from the outside.