CodeBlanks Logo Icon CodeBlanks

dartdoc

Learn documentation generation with dartdoc.

dartdoc

dartdoc is Dart's official documentation generator. It reads the doc comments (///) in your source code and produces HTML documentation.

Command: dart doc (new name) or dartdoc (legacy). Output is written to the doc/api/ folder by default.

Writing a doc comment for every public API in a package is good practice; dartdoc gathers them automatically.

The dart doc command

The dart doc command scans the files under lib/, gathers the /// comments and produces HTML in the doc/api/ folder.

You can change the output location with --output: dart doc --output build/docs. This gives output suited to build systems.

Open the generated HTML in a browser to browse your package's API docs.

Practice A doc comment for dartdoc

Write String greet(String name) => 'Hello $name';. Add a doc comment above it: /// Greets the name.. In main print print(greet('Alex')).

Starter code

main.dart
// Write the greet function and its doc comment

void main() {
  // print the greet('Alex') call
}

Sample solution

main.dart
/// Greets the name.
String greet(String name) => 'Hello $name';

void main() {
  print(greet('Alex'));
}

Expected output

Hello Alex

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does dartdoc produce?

  • HTML documentation Correct answer
  • Runnable code
  • Test report
  • Package file

Explanation: dartdoc builds HTML pages from doc comments.

2 The dart doc command

Question
dart ____ --output build/docs
Answer
dart doc --output build/docs

Explanation: dart doc generates documentation.

3 dartdoc parts

Term Meaning
dart doc Command
/// Doc comment
lib/ Scanned folder
HTML Output format

All lessons in Intermediate