Annotations in Dart
note
Dart supports single-line comments, multi-line comments, and document comments, which are slightly different from other languages. For example, ///
it is document comments. The details are as follows, let’s learn together.
Reference:
Annotations in Dart
single line comment
Single-line comments //
start with . Everything between //
and the end of the line is ignored by the compiler.
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
multiline comment
Multi-line comments /*
start with and */
end with . Everything between /*
and */
is ignored by the compiler (documentation comments are not ignored), and multi-line comments can be nested.
void main() {
/*
* This is a lot of work. Consider raising chickens.
Llama larry = Llama();
larry.feed();
larry.exercise();
larry.clean();
*/
}
Documentation comments
Documentation comments can be multi-line comments or single-line comments. Documentation comments start with ///
or /**
. Using on consecutive lines ///
has the same effect as a multi-line doc comment.
In documentation comments, the parser ignores all text unless enclosed in square brackets. Use square brackets to refer to classes, methods, fields, top-level variables, functions, and parameters. Symbols in parentheses are resolved in the lexical domain of the documented program element.
Here is a doc comment that references other classes and members:
/// A domesticated South American camelid (Lama glama).
///
/// Andean cultures have used llamas as meat and pack
/// animals since pre-Hispanic times.
///
/// Just like any other animal, llamas need to eat,
/// so don't forget to [feed] them some [Food].
class Llama {
String? name;
/// Feeds your llama [food].
///
/// The typical llama eats one bale of hay per week.
void feed(Food food) {
// ...
}
/// Exercises your llama with an [activity] for
/// [timeLimit] minutes.
void exercise(Activity activity, int timeLimit) {
// ...
}
}
In the generated documentation, [feed]
there will be a link pointing to feed
the documentation for the method, which [Food]
will be a link pointing to Food
the API documentation for the class.
To parse Dart code and generate HTML documentation, you can use Dart's documentation generation tool dart doc
. For examples of generating documentation, see the Dart API documentation . For recommendations on document structure, see the documentation: Guidelines for Dart Doc Comments .