×

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program, metadata is also used to gain more perspective about the code. Whenever we correspond with a new piece of code, we can get the related information with the help of metadata.

With a dart metadata annotation starts with a ‘ @ ’ sign, followed by a call to a constant constructor or a compile-time such as an output. ‘@Override ’ and ‘ @deprecated ’ are available for all dart codes.

We will be going through an example of ‘ @override ’ annotation to understand it more clearly –

class meta
{
  void code( ) 
  {
    print( " Now you are inside the ' meta ' class. " ) ;
  }
}
class meta1 extends meta 
{
  @override
  void code( ) 
  {
    print( " Now you are inside the ' meta1 ' class. " ) ;
  }
}
void main( ) 
{
  meta1 m1 = new meta1( ) ;
  m1.code( ) ;
}

Output:

Now you are inside the ' meta1 ' class.

Explanation:

From the code above, we can clearly see that the child class is crossing the path of the parent class. The ‘ @override ’ annotation makes this information even clearer. In addition to the dart annotations, we can create our own metadata annotation.

Example:

class Implement {
  final String what ;
  final String by ;
  const Implement( this.what, this.by ) ;
}
@Implement( ' the following function ', ' J1 ' )
void Func( ) {
  print( ' Implement me ' ) ;
}  
void main( ) {
  Func( ) ;
}

Output :

Implement me

Here we explained the annotation of ‘ @Implement ’ using a constant constructor. In particular it has two arguments, namely what should be used and the other stating who will be used and by whom.

Metadata can be used before the library, type parameter, function, constructor, class, etc., and before the import or export command. We can get metadata during operation with the help of reflection.

Metadata Class

1. Methods

  • noSuchMethod( Invocation invocation ) -> dynamic

Invoked when a non-existent method or property is accessed. Classes can override ‘ noSuchMethod( ) ’ method to provide custom behaviour. If a value is returned, it becomes the result of the original invocation.

Implementation :

         dynamic noSuchMethod (

Invocation invocation

)

  • toString( ) -> String

Returns a string representation of this object.

Implementation :

external String toString( ) ;

2. Properties

  • modificationTime -> DateTime

Implementation :

DateTime get modificationTime =>

    convertNativeToDart_DateTime( this._get_modificationTime ) ;

  • size -> int

Implementation :

final int size ;

  • hashCode -> int

This property retreived hash code for the object invoking it.

Implementation :

external int get hashCode ;

  • runtimeType -> Type

A representation of the runtime type of the object.

Implementation :

external Type get runtimeType ;

3. Operators

The equality operator.

  • Operator ==( dynamic other ) -> bool

Implementation :

external bool operator == ( other ) ;


Related Topics

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.