---
name: dart-best-practices
description: "Dart code quality conventions: naming, const/final/var hierarchy, single quotes, trailing commas, collection idioms, tear-offs, and import organization. Use when writing new Dart code or reviewing for style violations — wrong import style, global variables, var misuse, anonymous lambdas where tear-offs fit, or missing trailing commas. (triggers: **/*.dart, naming, convention, trailing comma, import, tear-off)"
---

# Dart Best Practices

## **Priority: P1 (OPERATIONAL)**

Best practices for writing clean, maintainable Dart code.

- **Scoping**:
  - No global variables.
  - Private globals (if required) must start with `_`.
- **Immutability**: Use `const` > `final` > `var`.
- **Config**: Use `--dart-define` for secrets. Never hardcode API keys.
- **Naming**: Follow [effective-dart](https://dart.dev/guides/language/effective-dart) (PascalCase classes, camelCase members).
- **Strings**: Prefer single quotes; use double quotes only for interpolation needs.
- **Trailing Commas**: Always use trailing commas for multi-line literals/params.
- **Expression Bodies**: Prefer `=>` for single-expression functions/getters.
- **Collections**:
  - Use `.map`, `.where`, `.fold`, `.any` over manual loops when clarity improves.
  - Type empty collections (`<String>[]`, `<String, User>{}`) to avoid `dynamic`.
  - Use collection `if`/`for` and spread operators for composable lists/maps.
- **Async**: Always `await` futures unless intentionally fire-and-forget.

```dart
import 'models/user.dart'; // Good
import 'package:app/models/user.dart'; // Avoid local absolute
```


## Anti-Patterns

- ❌ `var result = fetchOrders()` — use `final` or an explicit type; `var` only for locally obvious types in short scopes
- ❌ `import 'package:myapp/features/orders/order_model.dart'` within the same package — use relative imports for intra-package files
- ❌ Top-level mutable `List<Order> cachedOrders = []` — no global mutable state; use DI or encapsulate in a class
- ❌ `list.forEach((item) => doSomething(item))` — prefer tear-offs: `list.forEach(doSomething)`



---
Source attribution: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 SkillsHub Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard
Attribution basis: Original repository namespace preserved by package mirror

Fields of work: Development & Engineering
Roles: Backend Developer, DevOps / SRE, Frontend Developer, Full-Stack Developer, Mobile Developer
Specialties: Backend, Cross Platform, Frontend
Tools: Dart
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 4d0e550489fa7f50e3221690711d8449d584ed2782a1cba87656c695d5a2a099
Metadata reviewed: 2026-10-27
This metadata supersedes earlier Mega AI Hub catalog attribution. Original instruction and supporting files are preserved.
