Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create our own metadata in Dart?

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?

like image 794
Freewind Avatar asked Feb 04 '14 13:02

Freewind


People also ask

How do you use dart metadata?

In dart a metadata annotation starts with @ symbol, followed by a call to a constant constructor or a compile-time constant such as override. @override and @deprecated are available to all dart codes.

How do I make a custom annotation in darts?

First, create a class that will be used as metadata annotation. Then, use that class as annotation by calling the constructor preceded by @ . You can add annotation at class, field, method, or constructor.

What is meta data in annotation?

Metadata is data about your data - lets say you had a DB table full of entries. The rows or tuples are the data, and the columns would be the metadata; they define what each value is for any given row.

What is meta flutter?

meta library Null safety. Annotations that developers can use to express the intentions that otherwise can't be deduced by statically analyzing the source code. See also @deprecated and @override in the dart:core library. Annotations provide semantic information that tools can use to provide a better user experience.


1 Answers

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:

  1. A reference to a compile-time constant variable.
  2. 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.

like image 79
mezoni Avatar answered Sep 22 '22 02:09

mezoni