What does override do in Flutter?

Either in a Stateful or in a Stateless widget, we always see the annotation override before the build() method. Right? 

Have you ever thought about what does the annotation override mean?

Moreover, is it necessary at all?

Let’s consider a Dart class to see its implementation first.

After that we will discuss the code.

class Car {
  int accelerator;
  int _speed = 0;
  int get speed => _speed;
  int gear;

  Car(this.accelerator, this.gear);

  int brake(int applyPressureOnBrake) {
    _speed -= applyPressureOnBrake * 10;
    return _speed;
  }

  int accelerate(int pressureOnAccelerator) {
    _speed += 10 * pressureOnAccelerator;
    return _speed;
  }
}

void main() {
  var bike = Car(0, 0);
  print('Car: ${bike._speed}');
  bike._speed = bike.accelerate(2);
  print('When the pressure on Accelerator is 2, the speed is ${bike._speed}');
  bike._speed = bike.brake(2);
  print('When the pressure on Brake is 2, the speed is ${bike._speed}');
}

If we run this code, what do we find?

Let’s see the output.

Car: 0
When the pressure on Accelerator is 2, the speed is 20
When the pressure on Brake is 2, the speed is 0

In the last section we have discussed the private member variable in Dart. 

Therefore we’re not discussing it here. 

In any case, we can see that Dart has converted the value of an integer data type to string data type.

Now Dart core library has a toString() method. 

As a result, we can override that method, and write the above code as follows.

class Car {
  int accelerator;
  int _speed = 0;
  int get speed => _speed;
  int gear;

  Car(this.accelerator, this.gear);

  int brake(int applyPressureOnBrake) {
    _speed -= applyPressureOnBrake * 10;
    return _speed;
  }

  int accelerate(int pressureOnAccelerator) {
    _speed += 10 * pressureOnAccelerator;
    return _speed;
  }

  @override
  String toString() => 'Car: $_speed mph';
}

void main() {
  var bike = Car(0, 0);
  print(bike);
  bike._speed = bike.accelerate(2);
  print('When the pressure on Accelerator is 2, the speed is ${bike._speed}');
  bike._speed = bike.brake(2);
  print('When the pressure on Brake is 2, the speed is ${bike._speed}');
}

Run the code and we get the same output as above.

Car: 0 mph
When the pressure on Accelerator is 2, the speed is 20
When the pressure on Brake is 2, the speed is 0

Do we need to override?

We understand from the above examples one thing.

With the help of the annotation @override we can override a superclass member with the same name.

We can apply it to any instance method. As we see in the Flutter build() method.

Since we have overridden a method, that is okay. Dart and Flutter does not mention it as a bad practice.

However, for an instance field we consider it as a bad practice whatsoever.

Therefore use the override annotation with care. And in any case, according to the dart documentation, it is optional.

 Use it when the superclass is not under your control. Which is our case in the above code.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter

Comments

Leave a Reply