agentleFS
ZAidBinAsifZAidBinAsif/flutter-ai-starter/.cursor/rules/architecture.mdc

flutter-ai-starter / rules

Cursor rule0 starsChanged 5 months agoMITView on GitHub

Architecture, folder layout, layering, and dependency rules

The file

We split this architecture.mdc into the rules it gives an agent

8 rules. About 0 are common, 3 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use build_runner for codegen only — freezed, json_serializable, and Mockito mocks (generate-mocks skill).

    line 20
  2. Very specific:

    Do not introduce other state managers (Riverpod, Bloc, GetX, GetIt) or ORMs without user approval and an entry in docs/DECISIONS.md.

    line 22
  3. Very specific:

    Create GoRouter in app/router.dart.

    line 72
  4. Distinctive:

    Do not rebuild the entire router reactively on every frame.

    line 75
  5. Distinctive:

    No AI vendor SDK on client — backend Edge Functions when AI is enabled.

    line 83
  6. Very specific:

    No singletons via static.

    line 84
  7. Very specific:

    No business logic in build().

    line 85
  8. Distinctive:

    Check for duplicate functionality and reuse opportunities.

    line 90

≈ “Common” is an estimate: at least 1 in 5 of the 6,668 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
architecture.mdc
---
description: Architecture, folder layout, layering, and dependency rules
globs: ["lib/**/*.dart", "test/**/*.dart", "supabase/**/*.{sql,ts}"]
alwaysApply: false
---

# Architecture

## Stack (company default — pin versions in each app's `pubspec.yaml`)

| Layer | Choice |
|---|---|
| Mobile | Flutter stable, Dart 3.x |
| State & DI | **`package:provider`** + **`ChangeNotifier`** ViewModels |
| Routing | **go_router** (declarative; `context.go` / `context.push`) |
| Models | **freezed** + **json_serializable** (sealed unions for errors/states) |
| HTTP | dio + supabase_flutter (when backend enabled) |
| Local | hive_ce, flutter_secure_storage |

Use `build_runner` for **codegen only** — freezed, json_serializable, and Mockito mocks (`generate-mocks` skill). Not for state management (no Riverpod/codegen DI).

Do not introduce other state managers (Riverpod, Bloc, GetX, GetIt) or ORMs without user approval and an entry in `docs/DECISIONS.md`.

## Pattern: feature-first MVVM (Flutter official)

Organize by **feature**, not by file type. Matches [Flutter app architecture](https://docs.flutter.dev/app-architecture):

| Layer | Folder | Role |
|---|---|---|
| View | `presentation/screens`, `widgets` | UI only — observes ViewModel |
| ViewModel | `presentation/view_models` | `ChangeNotifier` — screen state & commands |
| Model | `data/` + optional `domain/` | Repositories, services, DTOs, entities |

**Domain layer (`domain/`)** — optional; see `docs/ARCHITECTURE.md` for when to skip vs add.

## Folder layout

```
lib/
  main.dart                  # entrypoint — calls bootstrap(), then runApp
  bootstrap.dart             # binding init, zone/error handling, flavor setup
  app/                       app.dart, router.dart, theme.dart, dependencies.dart
  core/                      config/ errors/ logging/ network/ storage/ utils/
  design_system/             tokens/ theme/ components/
  features/<feature>/
    data/                    repositories/, services/, models/
    domain/                  entities/, use_cases/          # optional
    presentation/            view_models/, screens/, widgets/
  services/                  analytics/ crash/ push/
  l10n/
```

## Dependency injection

- Wire dependencies in `app/dependencies.dart` with `MultiProvider` at the root (`runApp`).
- Expose **services** and **repositories** as `Provider<T>`.
- Expose **ViewModels** as `ChangeNotifierProvider<T>` (or `.value` in tests).
- Inject into ViewModels via constructor — use `context.read()` in `create:` callbacks, not globals.
- ViewModels receive repositories as **private** constructor params — views must not call repositories directly.

## Layering

- **Direction:** `presentation → domain (optional) → data`. Never the reverse.
- **Services** wrap one data source each. No state.
- **Repositories** orchestrate services. **Must not depend on each other.**
- Widgets never import `data/` directly.
- Cross-cutting services in `lib/services/` registered in `dependencies.dart`.
- Feature A never imports `features/feature_b/`.

## go_router + provider

- Create `GoRouter` in `app/router.dart`.
- Inject repositories/ViewModels in route `builder`s via `context.read()`.
- Auth redirect: pass a `ChangeNotifier` (or `Listenable`) as `refreshListenable` on `GoRouter` — call `notifyListeners()` when auth state changes.
- Do not rebuild the entire router reactively on every frame.

## Rules

- One screen / ViewModel / repository per file.
- `freezed` for DTOs, entities, sealed `Failure` / UI state unions.
- Repositories return `Future<Result<T, Failure>>`.
- No `Supabase.instance.client` from widgets — inject via `Provider`.
- No AI vendor SDK on client — backend Edge Functions when AI is enabled.
- No singletons via `static`.
- No business logic in `build()`.
- `setState` only for local ephemeral UI (focus, animation controllers).

## Before implementing

- Check for duplicate functionality and reuse opportunities.
- Files: `snake_case.dart`; classes: `PascalCase`; ViewModels: `<Feature>ViewModel`.

See `docs/ARCHITECTURE.md` and `docs/DECISIONS.md`.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.