Write dart

From cfug/flutter.cn · View interactive version

Write dart is an AI agent skill from the GitHub repository cfug/flutter.cn. Write, review, or refactor correct, idiomatic Dart code. Use for Dart source, packages, snippets, tests, public APIs, documentation examples, and modern language features. Pair with the write-flutter skill for Flutter-specific concerns.

How to use this skill

Skill file

Write Dart

Produce clear, correct, idiomatic Dart.

Reference the write-technical-docs skill when authoring

explanatory prose and documentation comments.

Two principles guide the code rules:

In the references, DO and DON'T are near-absolute,

PREFER and AVOID are strong defaults,

and CONSIDER requires judgment.

Match the project

Follow applicable analysis_options.yaml files for additional requirements.

check the lower SDK constraint in pubspec.yaml.

The installed SDK might support syntax that the package doesn't.

Core defaults

Apply these high-frequency rules while writing.

Naming

lowerCamelCase for members, variables, and constants,

and lowercase_with_underscores for files, packages, and import prefixes.

For example, write defaultTimeout, never DEFAULT_TIMEOUT.

such as isEmpty and canClose.

Never prefix an accessor with get.

Types

Signatures are API documentation.

initialized locals, lambda parameters, initializing formals,

and inferable type arguments.

Reserve dynamic for deliberately disabling static checking.

Null safety and immutability

including locals, fields, and top-level variables.

make the constructor const.

or lazily on first access.

If code needs to ask whether initialization happened,

use a nullable type and check for null instead.

return an empty collection or stream instead of null.

Use a nullable return type only when absence is a real state

that callers must distinguish.

Control flow and errors

so adding a new case becomes a compile-time error everywhere it's handled.

Never discard errors without handling them.

Use rethrow, not throw e.

Throw Error subtypes only for programming bugs and

don't catch them for recovery.

Functions and async

and for arguments that are unclear at the call site.

For example, write resize(width: 320, keepAspect: true).

and account for every returned Future.

Await or return the future.

If an operation is intentionally detached,

wrap it in unawaited(operation())

and make its error behavior deliberate.

Write maintainable code

Prefer a clear name to an explanatory comment.

sealed class hierarchies, extension types, and non-nullable types

instead of runtime conventions and documentation.

don't add parameters, options,

or abstractions without a concrete requirement.

Use modern Dart

Use modern features when they improve type safety or remove boilerplate

without obscuring the example.

Check the package's SDK constraint,

then read Modern Dart idioms

for version requirements and examples.

Validate

When an SDK and project are available,

format changed files, run dart analyze, and run the relevant tests.

Use flutter test instead of dart test for Flutter code.

For standalone snippets, check the relevant reference and

compile them when practical.

References

Read only the references relevant to the code you're writing or reviewing:

Naming identifiers, organizing imports and exports,

and layout decisions the formatter doesn't handle.

Read before writing doc comments or reviewing comment quality.

Imports, null handling, strings, collections, members,

constructors, errors, and async.

Public API design, including names, classes, signatures,

annotations, parameters, and equality.

Recent language features, version constraints, and examples.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.