|
| 1 | +--- |
| 2 | +applyTo: "app/lib/**/*.dart, modules/data/lib/**/*.dart, modules/domain/lib/**/*.dart, modules/common/lib/**/*.dart" |
| 3 | +--- |
| 4 | + |
| 5 | +# Flutter Project Coding Standards |
| 6 | + |
| 7 | +## Project Structure & Architecture |
| 8 | + |
| 9 | +Follow a strict modular architecture: |
| 10 | + |
| 11 | +- `/app/lib/`: Presentation layer (screens, widgets, routes). |
| 12 | +- `/modules/domain/lib/`: Core business logic (services, cubits, abstract repositories, models). |
| 13 | +- `/modules/data/lib/`: Data access (API clients, DTOs, data sources, and `Entity` classes). |
| 14 | +- `/modules/common/lib/`: Shared utilities, themes, constants, and mixins. |
| 15 | + |
| 16 | +### Service & Cubit Communication |
| 17 | + |
| 18 | +- Cubits **must not** communicate directly with one another. |
| 19 | +- Services coordinate between repositories and cubits for complex operations. |
| 20 | +- Widgets are responsible for observing their respective cubit's state via BlocProvider/BlocBuilder and triggering actions as needed. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Naming Conventions |
| 25 | + |
| 26 | +- **PascalCase** for class names, enums, and type aliases. |
| 27 | +- **camelCase** for variables, functions, and parameters. |
| 28 | +- Prefix **private members** with an underscore `_`. |
| 29 | +- Use **ALL_CAPS** for constants (e.g., `const MAX_ITEMS = 10;`). |
| 30 | +- **File names** must follow **snake_case** (e.g., `user_profile_cubit.dart`). |
| 31 | +- **Cubit files** must end with `_cubit.dart` and state files with `_state.dart`. |
| 32 | +- **Service files** must use PascalCase followed by `Service.dart` (e.g., `AuthService.dart`). |
| 33 | +- **Widget files** must end with `_page.dart` or `_widget.dart` depending on scope. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Data Module Guidelines |
| 38 | + |
| 39 | +The `/modules/data/lib/` folder is exclusively for data handling: |
| 40 | + |
| 41 | +- Data sources (API, DB) |
| 42 | +- DTOs |
| 43 | +- `Entity` classes |
| 44 | + |
| 45 | +### Entity Standards |
| 46 | + |
| 47 | +- Each entity class must end with `Entity`, e.g., `UserEntity`. |
| 48 | +- File names for entities must be snake_case: `user_entity.dart`. |
| 49 | +- Entities must implement **JSON parsing logic only**. |
| 50 | +- For fields like `price` or `amount` that may come as `int`, `double`, or `String`, use: |
| 51 | + |
| 52 | +```dart |
| 53 | +num parseFlexibleNumber(dynamic value) { |
| 54 | + if (value is num) return value; |
| 55 | + return num.tryParse(value.toString()) ?? 0; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Mapping |
| 60 | + |
| 61 | +- Each `Entity` must have a corresponding domain `Model` in `/modules/domain/lib/` used in business logic. |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## Error Handling |
| 66 | + |
| 67 | +- Use `try/catch` for all asynchronous operations. |
| 68 | +- Always log errors with contextual information using `debugPrint`, `log`, or your logging utility. |
| 69 | +- Avoid silently swallowing errors. |
| 70 | +- Prefer using custom error types (`AppException`, `NetworkException`, etc.) in repositories or use cases. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## State Management (Bloc/Cubit) |
| 75 | + |
| 76 | +- Use **Bloc/Cubit** for state management across all features. |
| 77 | +- Each feature must have its own cubit under `/modules/domain/lib/bloc/{feature}/`. |
| 78 | +- Each cubit must have corresponding state classes in `/modules/domain/lib/bloc/{feature}/`. |
| 79 | +- Services coordinate between repositories and cubits, located under `/modules/domain/lib/services/`. |
| 80 | +- Avoid shared/global cubits unless absolutely necessary. |
| 81 | +- Cubits manage the complete screen state through state transitions. |
| 82 | +- Use `BlocProvider`, `BlocBuilder`, `BlocListener`, or `BlocConsumer` to react to state changes in widgets. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## TODOs and Comments |
| 87 | + |
| 88 | +- Use `// TODO:` for areas where logic is incomplete or unclear. |
| 89 | +- Use `// FIXME:` for known bugs, temporary workarounds, or technical debt. |
| 90 | + |
| 91 | +--- |
0 commit comments