Dart Metadata
Additional information about the Dart program is specified using the Dart Metadata. It often begins with the @ symbol and is followed by a call to a constant constructor or a reference to the compile-time constant.
Creating Metadata Annotation
We are able to define custom metadata annotations in Dart.
Let’s examine the subsequent illustration:
Example
library student;
Class Student {
final String studentName;
final String rollno;
const Student(this.studentName, this.code);
}
And in the example that follows, the @student annotation is used.
import 'student.dart' ;
@Student('studentName', 'rollno')
void greetHii() {
print("Hii Welcome to Javatpoint");
}
In Dart, metadata is often defined immediately before an import or export directive and before a library name, class, typedef, field, type parameter, factory, function, constructor, parameter, or variable declaration. We can use runtime reflection to obtain the metadata.
Example
class Human{
@Overridden method
void run()
{
print("Human is running");
}
}
class Man extends Human{
@Overriding method
void run(){
print("Boy is running");
}
}
void main(){
Man m = new Man();
//This will call the child class version of run()
m.run();
}
Output
Boy is running
We have added information to the functions of the parent class and child class in the code above.