Skip to content

Instantly share code, notes, and snippets.

@Sp4Rx
Created June 28, 2020 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sp4Rx/804289f98f20376f9991fc426ad63b07 to your computer and use it in GitHub Desktop.
Save Sp4Rx/804289f98f20376f9991fc426ad63b07 to your computer and use it in GitHub Desktop.
Weird Dart Mixin
void main() {
Foo foo = Foo();
print(foo.fromA);
print(foo.fromB);
print("=====================");
Foo1 foo1 = Foo1();
print(foo1.fromC);
print(foo1.fromD);
print("=====================");
Foo2 foo2 = Foo2();
print(foo2.fromA);
print(foo2.fromB);
print(foo2.fromC);
print(foo2.fromD);
}
//-------------------------------------------
class Foo with A, B {
Foo() {
super.aValue = AValue('This is a value');
}
}
class Foo1 with C, D {
Foo1() {
super.aValue = AValue('This is a value');
}
}
class Foo2 with A, B, C, D {
Foo2() {
super.aValue = AValue('This is a value');
}
}
//-------------------------------------------
mixin A {
AValue aValue;
String get fromA => '${aValue.value} from A';
}
mixin B {
AValue aValue;
String get fromB => '${aValue.value} from B';
}
mixin C {
AValue _aValue;
set aValue(AValue aValue) {
_aValue = aValue;
}
String get fromC => '${_aValue.value} from C';
}
mixin D {
AValue _aValue;
set aValue(AValue aValue) {
_aValue = aValue;
}
String get fromD => '${_aValue.value} from D';
}
//-------------------------------------------
class AValue {
final String value;
AValue(this.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment