Introduction
Metadata is a type of data that provides information about the underlying data. It's information about information. Metadata can be used in Dart programming to provide more information about the code. With metadata, we may infer information about a new piece of code whenever we interact with it. A metadata annotation in Dart begins with the @ and then calls a constant constructor or a compile-time constant variable, for example, a compile-time constant like override used as @override. It will be available to all dart codes.
A library, part header, typedef, type parameter, class, constructor, factory, function, field, parameter, or variable declaration, as well as an import, export, or part directive, can all follow metadata.
Creating a MetaData Annotation
We can also create a custom annotation and provide some parameters to show the information about the data, and this whole is done by creating a constant constructor.
Example: in this example, we show the use of metadata.
Code:
class StudentMarks
{
// Information about the student's marks.
final double upperLimit;
final double lowerLimit;
// Here we are making the const constructor with upper and lower limit.
const StudentMarks(this.lowerLimit, this.upperLimit);
}
// Using this meta data is for remarks for students.
// MetaData for the function topRemark().
@StudentMarks(80, 100)
void topRemark()
{
print("Good Student.");
}
// MetaData for the function mediumRemark().
@StudentMarks(50, 79)
void mediumRemark()
{
print("Medium Student.");
}
// MetaData for the function lowRemark().
@StudentMarks(0, 49)
void lowRemark()
{
print("Need to focus on study.");
}
void main()
{
double marks = 88;
if(marks >= 80){
topRemark();
}
else if(marks >= 50 && marks <= 79){
mediumRemark();
}
else{
lowRemark();
}
}
Output:
Good Student.
Explanation: Here we use metadata for the functions topRemark(), mediumRemark() and lowRemark(). The metadata shows the upper and lower limits of marks for the remark.





