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 从公开仓库中收录这些内容。

举报或申请下架