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 ) ;