×

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 Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

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.

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 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.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 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 Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 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 If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.