Agent Skills › flutter/agent-plugins › dart-use-doc-examples

dart-use-doc-examples

GitHub

指导如何在Dartdoc中通过{@example}指令注入外部代码示例,利用#hide、#region和#endregion标签过滤展示内容,确保文档示例可分析、Lint和执行。

skills/dart-use-doc-examples/SKILL.md flutter/agent-plugins

Trigger Scenarios

编写 Dart 包文档 优化 API 文档中的代码示例展示 清理文档中的冗余或敏感代码

Install

npx skills add flutter/agent-plugins --skill dart-use-doc-examples -g -y
More Options

Use without installing

npx skills use flutter/agent-plugins@dart-use-doc-examples

指定 Agent (Claude Code)

npx skills add flutter/agent-plugins --skill dart-use-doc-examples -a claude-code -g -y

安装 repo 全部 skill

npx skills add flutter/agent-plugins --all -g -y

预览 repo 内 skill

npx skills add flutter/agent-plugins --list

SKILL.md

Frontmatter
{
    "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."
}

Using Examples in Dartdoc

Contents

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.

1. The {@example} Directive

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]}

  • <path>: The path to the file. A leading / evaluates from the package root. Otherwise, it is relative to the current file.
  • lang: The language for the markdown fence. Auto-detected from the file extension (e.g., dart), but can be explicit (e.g., lang=text).
  • indent: strip (default) aggressively removes shared leading indentation from the code block.

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}

2. Using Regions

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}

3. Hiding Setup Code

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.

4. Marker Filtering Rules

When working with #hide, #region, and #endregion markers, you must follow these two technical constraints:

  • Region Required: The markers are only processed and stripped when you target a specific region suffix (e.g., {@example file.dart#region_name}). If you inject an entire file without a region suffix, the file is embedded exactly as it appears in the source, including any marker text like // #hide.
  • Format Agnosticism: The marker system is completely format-agnostic. Dartdoc simply runs a regex to strip lines containing the marker strings, meaning it works identically in non-Dart files (e.g., inside YAML comments # #region or HTML comments <!-- #region -->).

5. Placement and Path Resolution

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:

  • Package-Root Paths (/): Paths starting with a leading slash automatically resolve directly to the root of the Dart package. Use this when the destination file is deep.
    • Example: {@example /test/data/sample.txt} exactly maps to <package_root>/test/data/sample.txt.
  • Relative Paths: Paths without a leading slash resolve relative to the directory of the file containing the doc comment.
    • Example: {@example ../utils/demo.dart}
  • Boundary Enforcement: Using .. segments to traverse upward is perfectly acceptable, but dartdoc natively stops directory traversal at the package root (it will never escape the package).
  • No Network URLs: Absolute URIs (e.g., starting with https://) are strictly not supported. The example file must sit natively somewhere in the local filesystem.
  • Separators & Encoding: Because dartdoc resolves the path as a URI, you must always use forward slashes (/) as folder separators (even on Windows). You can natively include URI-encoded characters (like %20 for spaces) as permitted by URI reference rules.

6. Verification

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.

Version History

  • e89522a Current 2026-09-22 20:14

Same Skill Collection

.agents/agents/reidbaker-agent/skills/api-review/SKILL.md
.agents/agents/reidbaker-agent/skills/code-documentation/SKILL.md
.agents/agents/reidbaker-agent/skills/code-review/SKILL.md
.agents/agents/reidbaker-agent/skills/grill-with-docs/SKILL.md
.agents/agents/reidbaker-agent/skills/natural-writing/SKILL.md
.agents/agents/reidbaker-agent/skills/unix-cli-best-practices/SKILL.md
skills/dart-add-unit-test/SKILL.md
skills/dart-build-cli-app/SKILL.md
skills/dart-collect-coverage/SKILL.md
skills/dart-fix-runtime-errors/SKILL.md
skills/dart-generate-test-mocks/SKILL.md
skills/dart-migrate-to-checks-package/SKILL.md
skills/dart-resolve-package-conflicts/SKILL.md
skills/dart-run-static-analysis/SKILL.md
skills/dart-use-ffigen/SKILL.md
skills/dart-use-path-package/SKILL.md
skills/dart-use-pattern-matching/SKILL.md
skills/dart-use-primary-constructors/SKILL.md
skills/dart-write-documentation/SKILL.md
skills/flutter-add-integration-test/SKILL.md
skills/flutter-add-widget-preview/SKILL.md
skills/flutter-add-widget-test/SKILL.md
skills/flutter-apply-architecture-best-practices/SKILL.md
skills/flutter-build-responsive-layout/SKILL.md
skills/flutter-fix-layout-issues/SKILL.md
skills/flutter-implement-json-serialization/SKILL.md
skills/flutter-setup-declarative-routing/SKILL.md
skills/flutter-setup-localization/SKILL.md
skills/flutter-use-http-package/SKILL.md
tool/dart_skills_lint/.agents/skills/add-dart-lint-validation-rule/SKILL.md
tool/dart_skills_lint/.agents/skills/check-downstream-consumers/SKILL.md
tool/dart_skills_lint/.agents/skills/contributor-pr-description/SKILL.md
tool/dart_skills_lint/.agents/skills/dart-skills-lint-integration/SKILL.md
tool/dart_skills_lint/.agents/skills/definition-of-done/SKILL.md
tool/dart_skills_lint/.agents/skills/run-evals/SKILL.md
tool/dart_skills_lint/example/skills/invalid/SKILL.md
tool/dart_skills_lint/example/skills/valid/SKILL.md
tool/dart_skills_lint/skills/dart-skills-lint-setup/SKILL.md
tool/dart_skills_lint/skills/dart-skills-lint-validation/SKILL.md
skills/dart-setup-ffi-assets/SKILL.md

Metadata

Files
0
Version
3f58a55
Hash
30b456ce
Indexed
2026-09-22 20:14

ホーム - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-29 18:24
浙ICP备14020137号-1