I was reading a class like:
class SplashScreenState extends State<SplashScreen>
with SingleTickerProviderStateMixin {
...
...
}
And I am just wondering what with keyword refers in this context and why do we use it ?
The with keyword indicates the use of a "mixin". See here. A mixin refers to the ability to add the capabilities of another class or classes to your own class, without inheriting from those classes. The methods of those classes can now be called on your class, and the code within those classes will execute.
It is similar to the reuse you get from extending a class, but is not multiple inheritances. There still only exists one superclass. With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.
The this keyword is used to point the current class object. It can be used to refer to the present class variables. We can instantiate or invoke the current class constructor using this keyword. We can pass this keyword as a parameter in the constructor call.
In the Flutter SDK the . of methods are a kind of service locator function that take the framework BuildContext as an argument and return an internal API related to the named class but created by widgets higher up the widget tree.
I got the answer from here.
I will break it down
The concept i was looking for is called Mixins
So what are mixins ?
Mixins are a way of reusing a class’s code in multiple class hierarchies, In other words when we need a functionality and we cannot implement it in one of a super class , or it does not make sense to do so
If it's a bit hard to understand let's look at following example
We have the following diagram
As yellow square refers to the ability of walking And blue square ...... as described in the above diagram
We have here a superclass called Animal which has three subclasses (Mammal, Bird, and Fish)
Some animals share common behavior: A cat and a dove can both walk, but the cat cannot fly. These kinds of behavior are orthogonal to this classification, so we cannot implement these behavior in the superclasses. If a class could have more than one superclass, it would be easy, we could create three other classes: Walker, Swimmer, Flyer. After that, we would just have to inherit Dove and Cat from the Walker class. But in Dart, every class (except for Object) has exactly one superclass. Instead of inheriting from the Walker class, we could implement it, as it if was an interface, but we should have to implement the behavior in multiple classes, so it’s not a good solution.
So here comes the use of with
Let's define the walker class
class Walker {
void walk() {
print("I'm walking");
}
}
And to solve the above issue we make use of mixins
class Cat extends Mammal with Walker {}
class Dove extends Bird with Walker, Flyer {}
Now if we call
main(List<String> arguments) {
Cat cat = Cat();
Dove dove = Dove();
// A cat can walk.
cat.walk();
// A dove can walk and fly.
dove.walk();
dove.fly();
// A normal cat cannot fly.
// cat.fly(); // Uncommenting this does not compile.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With