dart-add-unit-test
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains…
How to inject external code examples into Dartdoc using the {@example} directive, and how to filter those files using #hide, #region, and #endregion tags.
$ npx -y skills add dart-lang/skills --skill dart-use-doc-examples --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/dart-use-doc-examplesContext preview
The summary Claude sees to decide when to auto-load this skill.
How to inject external code examples into Dartdoc using the {@example} directive, and how to filter those files using #hide, #region, and #endregion tags.
name: dart-use-doc-examples
description: "How to inject external code examples into Dartdoc using the {@example} directive, and how to filter those files using #hide, #region, and #endregion tags."When writing documentation that requires multi-line code examples, you should generally extract those examples into standalone `.dart` files and inject them using the `{@example}` directive, rather than writing them inline inside `///` comments. This ensures the examples can be analyzed, linted, and executed.
The `{@example}` directive parses an external file and resolves it into a fenced Markdown code block in the generated documentation.
**Syntax:** `{@example <path>[#<region>] [lang=LANGUAGE] [indent=keep|strip]}`
*Bad (Inline Markdown):*
/// Makes a client service request to the backend. /// /// ```dart /// final client = Client(); /// client.send(); /// ```
*Good (External File Injection):*
/// Makes a client service request to the backend.
///
/// {@example /example/client_request.dart}Often, an external example file contains imports, setup, or `void main()` wrappers that you don't want to show in the documentation. You can extract a specific block of code by appending `#<region>` to the `{@example}` directive path, and wrapping that code with `#region` and `#endregion` comments in the target file.
**Dart Code (e.g., `/example/client.dart`):**
import 'package:http/http.dart';
void main() {
// #region request_snippet
final client = Client();
client.send();
// #endregion request_snippet
}**Dartdoc Usage:**
/// Connects the client to the server and sends a request.
///
/// {@example /example/client.dart#request_snippet}If there is a specific line of code within your extracted region that is necessary for the compiler/analyzer to pass but irrelevant (or distracting) for the documentation reader, append `#hide` to that line.
**Dart Code:**
final mockServer = startServer(); // #hide final data = await fetch(mockServer.url);
In the generated documentation, only `final data = await fetch(mockServer.url);` will be visible. The line with `#hide` is completely dropped.
When working with `#hide`, `#region`, and `#endregion` markers, you must follow these two technical constraints:
The `{@example}` directive is a block-level directive. It must appear on its own line prefixed with `///`. Its internal `<path>` parser follows strict URI reference rules:
After injecting examples: 1. Run `dart analyze` on the example files to ensure the hidden setup code compiles. 2. (Optional) Run `dart doc` to verify that dartdoc successfully parsed the directive without throwing a "Failed to read file" or "missing region" warning.
Agent skills for Dart, maintained by the Dart team. A collection of skills providing tailored instructions for common Dart development workflows.
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains…
Architectural patterns, entrypoint structure, exit codes, stream routing, and subprocess spawning for Dart command-line interface (CLI) applications. Use when…
Collect coverage using the coverage packge and create an LCOV report
Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex…
Replace the usage of `expect` and similar functions from `package:matcher` to `package:checks` equivalents.