Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"with" keyword in Flutter

Tags:

flutter

dart

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 ?

like image 534
Abdulrahman Falyoun Avatar asked Jul 28 '19 18:07

Abdulrahman Falyoun


People also ask

What is with keyword in Dart?

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.

What is difference between with and extends in Flutter?

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.

How do I use this keyword in Flutter?

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.

What is of () in Flutter?

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.


1 Answers

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

Classes

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.
}

like image 150
Abdulrahman Falyoun Avatar answered Oct 21 '22 09:10

Abdulrahman Falyoun