Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Is The Purpose of @override used in Flutter?

Tags:

flutter

dart

In the flutter documentation the keyword @override is used a lot in classes. I tried to search for the meaning but still can't understand. What is the purpose of this @override Keyword.

Note: I come from a JavaScript background.

like image 399
claOnline Avatar asked Jul 07 '18 14:07

claOnline


People also ask

Why is @override used in flutter?

The annotation @override marks an instance member as overriding a superclass member with the same name. The annotation applies to instance methods, getters and setters, and to instance fields, where it means that the implicit getter and setter of the field is marked as overriding, but the field itself is not.

What is @override used for?

The @Override annotation indicates that the child class method is over-writing its base class method. It extracts a warning from the compiler if the annotated method doesn't actually override anything. It can improve the readability of the source code.

Is @override necessary in Dart?

In any case, the use of @override is optional. For example, the annotation is intentionally not used in the Dart platform libraries, since they only depend on themselves.

What does override do in Dart?

Method overriding occurs in dart when a child class tries to override the parent class's method. When a child class extends a parent class, it gets full access to the methods of the parent class and thus it overrides the methods of the parent class.


1 Answers

@override just points out that the function is also defined in an ancestor class, but is being redefined to do something else in the current class. It's also used to annotate the implementation of an abstract method. It is optional to use but recommended as it improves readability.

The annotation @override marks an instance member as overriding a superclass member with the same name.

https://api.dartlang.org/stable/1.24.3/dart-core/override-constant.html

JavaScript also supports @override as an annotation, but it has to be in a comment. http://usejsdoc.org/tags-override.html.

like image 117
Jacob Phillips Avatar answered Sep 17 '22 21:09

Jacob Phillips