Metadata
Use metadata to give additional information about your code. A metadataannotation begins with the character @
, followed by either a referenceto a compile-time constant (such as deprecated
) or a call to aconstant constructor.
Two annotations are available to all Dart code: @deprecated
and@override
. For examples of using @override
,see Extending a class.Here’s an example of using the @deprecated
annotation:
- class Television {
- /// _Deprecated: Use [turnOn] instead._
- @deprecated
- void activate() {
- turnOn();
- }
- /// Turns the TV's power on.
- void turnOn() {...}
- }
You can define your own metadata annotations. Here’s an example ofdefining a @todo annotation that takes two arguments:
library todo;
class Todo {
final String who;
final String what;
const Todo(this.who, this.what);
}
And here’s an example of using that @todo annotation:
import 'todo.dart';
@Todo('seth', 'make this do something')
void doSomething() {
print('do something');
}
Metadata can appear before a library, class, typedef, type parameter,constructor, factory, function, field, parameter, or variabledeclaration and before an import or export directive. You canretrieve metadata at runtime using reflection.