flutter-dart-code-review
它能做什么
这是一份用于审查 Flutter 和 Dart 代码的指南。无论你使用哪种状态管理库,它都会检查代码的组织是否清晰、编程实践是否良好,以及是否避开了常见错误。
适用场景
- ✓审查别人编写的代码时。
- ✓功能开发完成、定稿之前。
- ✓想学习 Flutter/Dart 最佳实践时。
技能链接
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/flutter-dart-code-review/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请审查这段 Flutter 代码,重点看组织结构的最佳实践以及潜在错误。”
每周 5 个新技能,1 分钟读完。
工作原理
指南会检查项目结构、Dart 语言的正确用法、创建组件(界面中的可视部分)的最佳实践,以及状态(会变化的数据)的管理方式。它能确保代码高效、易于访问,并且足够安全。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Flutter/Dart Code Review Best Practices
Comprehensive, library-agnostic checklist for reviewing Flutter/Dart applications. These principles apply regardless of which state management solution, routing library, or DI framework is used.
1. General Project Health
- Project follows consistent folder structure (feature-first or layer-first)
- Proper separation of concerns: UI, business logic, data layers
- No business logic in widgets; widgets are purely presentational
-
pubspec.yamlis clean — no unused dependencies, versions pinned appropriately -
analysis_options.yamlincludes a strict lint set with strict analyzer settings enabled - No
print()statements in production code — usedart:developerlog()or a logging package - Generated files (
.g.dart,.freezed.dart,.gr.dart) are up-to-date or in.gitignore - Platform-specific code isolated behind abstractions
2. Dart Language Pitfalls
- Implicit dynamic: Missing type annotations leading to
dynamic— enablestrict-casts,strict-inference,strict-raw-types - Null safety misuse: Excessive
!(bang operator) instead of proper null checks or Dart 3 pattern matching (if (value case var v?)) - Type promotion failures: Using
this.fieldwhere local variable promotion would work - Catching too broadly:
catch (e)withoutonclause; always specify exception types - Catching
Error:Errorsubtypes indicate bugs and should not be caught - Unused
async: Functions markedasyncthat neverawait— unnecessary overhead -
lateoveruse:lateused where nullable or constructor initialization would be safer; defers errors to runtime - String concatenation in loops: Use
StringBufferinstead of+for iterative string building - Mutable state in
constcontexts: Fields inconstconstructor classes should not be mutable - Ignoring
Futurereturn values: Useawaitor explicitly callunawaited()to signal intent -
varwherefinalworks: Preferfinalfor locals andconstfor compile-time constants - Relative imports: Use
package:imports for consistency - Mutable collections exposed: Public APIs should return unmodifiable views, not raw
List/Map - Missing Dart 3 pattern matching: Prefer switch expressions and
if-caseover verboseischecks and manual casting - Throwaway classes for multiple returns: Use Dart 3 records
(String, int)instead of single-use DTOs -
print()in production code: Usedart:developerlog()or the project's logging package;print()has no log levels and cannot be filtered
3. Widget Best Practices
Widget decomposition:
- No single widget with a
build()method exceeding ~80-100 lines - Widgets split by encapsulation AND by how they change (rebuild boundaries)
- Private
_build*()helper methods that return widgets are extracted to separate widget classes (enables element reuse, const propagation, and framework optimizations) - Stateless widgets preferred over Stateful where no mutable local state is needed
- Extracted widgets are in separate files when reusable
Const usage:
-
constconstructors used wherever possible — prevents unnecessary rebuilds -
constliterals for collections that don't change (const [],const {}) - Constructor is declared
constwhen all fields are final
Key usage:
-
ValueKeyused in lists/grids to preserve state across reorders -
GlobalKeyused sparingly — only when accessing state across the tree is truly needed -
UniqueKeyavoided inbuild()— it forces rebuild every frame -
ObjectKeyused when identity is based on a data object rather than a single value
Theming & design system:
- Colors come from
Theme.of(context).colorScheme— no hardcodedColors.redor hex values - Text styles come from
Theme.of(context).textTheme— no inlineTextStylewith raw font sizes - Dark mode compatibility verified — no assumptions about light background
- Spacing and sizing use consistent design tokens or constants, not magic numbers
Build method complexity:
- No network calls, file I/O, or heavy computation in
build() - No
Future.then()orasyncwork inbuild() - No subscription creation (
.listen()) inbuild()
…
常见问题
“与库无关”是什么意思?
意思是这些建议适用于你在 Flutter 项目中使用的任何状态管理库,比如 BLoC、Riverpod、Provider 等。
为什么最终代码中不要使用 `print()`?
`print()` 会让应用的日志变得杂乱,而且没有日志级别,很难分辨哪些是重要信息。更好的做法是使用专门的日志工具。
什么是组件(widgets)?
组件是 Flutter 构建用户界面的基本模块,代表文本、按钮、布局等可视元素。
详情
作者的原始描述
Library-agnostic Flutter/Dart code review checklist covering widget best practices, state management patterns (BLoC, Riverpod, Provider, GetX, MobX, Signals), Dart idioms, performance, accessibility, security, and clean architecture. Use when reviewing Flutter or Dart code, whatever state management library the project uses.
repo: affaan-m/ECC
热度
- GitHub 星标
- 280k
- 排名
- #180
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
accessibility
280k帮你的智能体创建人人都能使用的界面。确保网站和应用对残障人士友好,比如失明或行动不便的用户也能顺畅使用。
agent-architecture-audit
280k让你的智能体检查一切是否正常运行。它能找出并修复 AI 系统中的问题,确保系统按预期工作。
agent-eval
280k帮你的智能体比较不同的 AI 编程工具。它能衡量哪个工具在特定任务上更快、更便宜、更可靠。
agent-harness-construction
280k让你的智能体更好地理解如何完成任务。它会优化智能体使用的工具和指令,提高成功的几率。