dart-async-programming
Installation
SKILL.md
Writing Asynchronous Dart Code
Contents
Core Guidelines
Write asynchronous Dart code using modern, declarative patterns. Avoid legacy callback-based approaches. Assume all network, file I/O, and database operations are asynchronous.
- Use
async/await: Always preferasyncandawaitover raw.then(),.catchError(), or.whenComplete()chains. This flattens the execution flow and improves readability. - Handle Errors Gracefully: Wrap all
awaitcalls intry-catchblocks to handle exceptions. Do not rely on unhandled future rejections. - Execute Concurrently: Use
Future.waitto initiate and await multiple independent futures concurrently rather than awaiting them sequentially. - Consume Streams Sequentially: Prefer
await forover.forEach()or.listen()when consuming streams, unless you specifically need low-level subscription management (like pausing or resuming). - Prevent Memory Leaks: Always call
.close()on aStreamControllerwhen it is no longer needed or when the owning class is disposed.