I want to create some own metadata for my dart codem, e.g. @table, @column, but I can't find any useful documents about it.
But I do found there are some special metadata (e.g. NgController) in angular.dart: https://github.com/angular/angular.dart/blob/master/demo/todo/web/todo.dart#L52
How to create my own metadata in Dart? Is there any documents?
Dart supports metadata which is used to attach user defined annotations to program structures.
Metadata consists of a series of annotations, each of which begin with the character @, followed a constant expression that starts with an identifier. It is a compile time error if the expression is not one of the following:
- A reference to a compile-time constant variable.
- A call to a constant constructor.
Metadata can appear before a library, part header, class, typedef, type parameter, constructor, factory, function, field, parameter, or variable declaration and before an import, export or part directive.
So, suggested by you constants such @table
, @column
are very limited by functionality because they cannot hold additional information (parameters).
@DataTable("sale_orders")
class SaleOrder {
@DataColumn("sale_order_date")
DateTime date;
}
@table
class Product {
@column
String name;
}
const DataColumn column = const DataColumn();
const DataTable table = const DataTable();
class DataTable {
final String name;
const DataTable([this.name]);
}
class DataColumn {
final String name;
const DataColumn([this.name]);
}
But in any case, you choose the option that best suits your needs.
This is an interesting blog post about Dart annotations
http://japhr.blogspot.co.at/2013/01/i-love-dart-annotations.html
Here the meta data part of the Dart language specification
https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.d0rowtffuudf
Metadata consists of a series of annotations, each of which begin with the character @, followed a constant expression that starts with an identifier.
So you can use a class with a const constructor as annotation.
- Dart Up and Running
https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-metadata