In this tutorial you will learn about the Dart Metadata and its application with practical example.
Dart Metadata
In Dart, metadata is used to provide additional information about your dart program. A metadata annotation usually begins with @ character, followed by either a reference to a compile-time constant or a call to a constant constructor.
Note:- The @deprecated and @override are available for all Dart Program.
Creating Metadata Annotations
In Dart, you are allowed to define your own metadata annotations.
Example:-
Here’s an example of defining a @employee annotation that takes two arguments –
1 2 3 4 5 6 7 8 |
library employee; class Employee{ final String department; final String code; const Employee(this.department, this.code); } |
And here’s an example of using that @employee annotation –
1 2 3 4 5 6 |
import 'employee.dart'; @Employee('Admin', 'ADM') void sayHello() { print("Hello World!'); } |
In Dart, metadata usually appear before a library, class, typedef, type parameter, constructor, factory, function, field, parameter, or variable declaration and before an import or export directive. Metadata can be retrieved using reflection in runtime.