Dart Best Practices

kevmoo/dash_skills/skills/dart-best-practices

作者 kevmoo397a703060ec8449b4cee63749036f19662b1f0eApache-2.0147 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫今天更新

General best practices for Dart development. Covers code style, effective Dart, and language features.

AI 產生的概覽

提供 Dart 慣用程式碼風格、Effective Dart 慣用法與語言特性建議的指引。

功能
提供 Dart 開發的一般最佳實務,涵蓋程式碼風格、Effective Dart 慣用法與語言特性建議。它提供具體的正反範例,例如偏好使用多行字串而非字串串接,並將行寬控制在 80 個字元以內。它也列出探索線索,包括正規表示式與 lines_longer_than_80_chars lint,並提供不應重構的棄權護欄。
適用情境
適用於撰寫或審查 Dart 程式碼,或需要慣用 Dart 用法指引時。它也用于尋找可改為多行字串與可修正行長的候選位置。不應套用於公開 API 破壞性變更、教條式微最佳化、產生的檔案或公開介面中的明確型別註解。
執行需求
不需要指令碼或特殊工具,僅為說明性內容。探索指引引用了 Dart 分析器的 lines_longer_than_80_chars lint。

Dart Best Practices

1. When to use this skill

Use this skill when:

  • Writing or reviewing Dart code.
  • Looking for guidance on idiomatic Dart usage.

When NOT to use (Abstention Guardrails)

Do NOT apply this skill or refactor code when:

  • Public API Breaking Changes: Refactoring would alter public API contracts, return types, or parameter signatures in published packages without a coordinated major SemVer bump.
  • Dogmatic Micro-Optimizations: Rewriting clear, readable code for negligible theoretical gains (e.g. replacing clear string concatenation in a simple one-line log message with multiline quotes).
  • Domain-Specific or Code-Generated Files: Generated files (*.g.dart, *.freezed.dart, protobufs) or files where line lengths and string layouts are managed by code generators.
  • Explicit Type Annotations in Public Interfaces: Replacing explicit type annotations with var or final where explicit types document the public API surface or disambiguate complex generics.

2. Best Practices

Multi-line Strings

Prefer using multi-line strings (''') over concatenating strings with + and \n, especially for large blocks of text like SQL queries, HTML, or PEM-encoded keys. This improves readability and avoids lines_longer_than_80_chars lint errors by allowing natural line breaks.

Avoid:

dart
final pem = '-----BEGIN RSA PRIVATE KEY-----\n' +    base64Encode(fullBytes) +    '\n-----END RSA PRIVATE KEY-----';

Prefer:

dart
final pem = '''-----BEGIN RSA PRIVATE KEY-----${base64Encode(fullBytes)}-----END RSA PRIVATE KEY-----''';

Line Length

Avoid lines longer than 80 characters, even in Markdown files and comments. This ensures code is readable in split-screen views and on smaller screens without horizontal scrolling.

Prefer: Target 80 characters for wrapping text. Exceptions are allowed for long URLs or identifiers that cannot be broken.

Discovery

Multi-line Strings

To find candidates for multi-line strings, search for string concatenation with + involving newlines:

  • Regex: ['"]\s*\+\s*['"]
  • Regex: \+\s*['"].*\\n

Line Length

  • Rely on the lines_longer_than_80_chars lint from the analyzer.

Related Skills

  • dart-modern-features: For idiomatic usage of modern Dart features like Pattern Matching (useful for deep JSON extraction), Records, and Switch Expressions.

來源與署名

來源:kevmoo/dash_skills位於skills/dart-best-practices提交397a703

授權條款: Apache-2.0

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架