Dart metadata :
A metadata starts with an @ character followed by the name of the metadata. Metadata is used to provide extra information in code. For example, if you override one method, @override is used above the overridden method name. This is a metadata annotation. We can create one annotation in dart.
Metadata is created using a compile-time constant or call to a constant constructor.
Example of dart metadata :
Mostly used dart metadata are deprecated and override. For example :
class MainClass {
getClassname() {
return 'MainClass';
}
}
class SubClass extends MainClass {
getClassname() {
return super.getClassname();
}
}
SubClass class is extending MainClass and it is overriding the getClassname method. Here, @override is metadata.
Metadata can be used before a class, typedef, constructor, factory, parameter, field, function, library, type parameter, variable declaration or before an import or export directive.
Custom metadata :
As I have explained above, we can use one constant constructor to create one custom metadata. For example :
class Student {
final String name;
final int age;
const Student(this.name, this.age);
}
This class has one constant constructor that takes two parameters. We can use this as metadata like below :
('Alex', 20)