Table of contents
1.
Introduction
2.
Creating a MetaData Annotation
3.
Example of MetaData in Dart
4.
Frequently Asked Questions
4.1.
What is metadata in simple words?
4.2.
What is compile-time constant metadata?
4.3.
What is constant constructor metadata?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Meta Data in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Example of MetaData in Dart

Since we have learned about how to create a constant constructor metadata data. Now, see the use of a compile-time constant variable.

Example: The example shown below uses the override annotation showing the use of metadata.

Code:

class Demo
{
  // Method of the base class
  void method(String name) 
  {
    print( "Hello $name, you are inside the 'Demo' class." ) ;
  }
}


class DemoChild extends Demo
{
  // 'DemoChild' class inheriting the 'Demo' class.
  // Method of the base class, but we are overriding it here.
  @override
  void method(String name) 
  {
    print( "Hello $name, you are inside the 'DemoChild' class." ) ;
  }
}


// Driver Code
void main( ) 
{
  // Creating the object of the 'DemoChild' class.
  DemoChild obj = DemoChild( ) ;
  obj.method("Ninja") ;
}


Output:

Hello Ninja, you are inside the 'DemoChild' class.


Explanation: Here, we are overriding a method whose name is "method" and to show this overriding, we use override annotation. 

Frequently Asked Questions

What is metadata in simple words?

Metadata is "data that offers information about other data," but not the data itself, such as a message's text or a picture.

What is compile-time constant metadata?

Compile-time constant metadata uses a keyword that is already defined in the compile, and we need not bother about defining the keyword. For example, @override and @deprecated, can use these metadata for our dart codes.

What is constant constructor metadata?

Constant constructor metadata is metadata that does not use a keyword instead, here, we have to design our custom metadata, and we can also add parameters to our custom-built metadata.

Conclusion

In this article, we've extensively discussed the MetaData in dart and its implementation. We have discussed how to use compile-time metadata and create constant constructor metadata.

We hope this blog has helped you enhance your knowledge regarding the MetaData in dart. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass