Dart汇总请点击这里
文章目录
使用注解提供有关代码的其他信息。元数据注解以字符
@
开头,后跟对编译时常量的引用(例如
deprecated
)或对常量构造函数的调用。
所有 Dart 代码都有四个注释: @Deprecated
、@deprecated
、@override
和@pragma
。有关使用 的示例@override
。以下是使用注释的示例@Deprecated
:
class Television {
/// Use [turnOn] to turn the power on instead.
('Use turnOn instead')
void activate() {
turnOn();
}
/// Turns the TV's power on.
void turnOn() {...}
// ···
}
@deprecated
如果您不想指定消息,则可以使用。但是,我们建议始终使用 来指定消息@Deprecated
。
您可以定义自己的元数据注释。以下是定义带有两个参数的注释的示例@Todo
:
class Todo {
final String who;
final String what;
const Todo(this.who, this.what);
}
以下是使用该注释的示例@Todo
:
('Dash', 'Implement this function')
void doSomething() {
print('Do something');
}
元数据可以出现在库、类、typedef、类型参数、构造函数、工厂、函数、字段、参数或变量声明之前以及导入或导出指令之前。