Flutter Best Practices
Standards for building maintainable Flutter apps, distilled from the official
Flutter architecture guide and LeanCode's experience shipping 40+ Flutter
projects (including a 30-developer banking app). Apply these when writing new
code; when touching existing code, prefer consistency with the surrounding
codebase and raise conflicts with these standards rather than silently
rewriting.
How to use this skill
Read the reference file that matches the task. Read more than one when tasks
overlap (e.g. a new feature touches both structure and coding style).
| Task |
Read |
| Design layers, decide where logic lives, MVVM, repositories/services |
references/architecture-layers.md |
| Create/organize a feature, folder structure, state management wiring |
references/feature-structure.md |
| Write or review Dart/Flutter code, widgets, tests, lints |
references/dart-coding-practices.md |
| Multi-team/multi-package apps, monorepo, navigation, localization, API contracts, E2E tests |
references/enterprise-scale.md |
| App localization setup, reusable UI package string ownership, language picker visibility |
references/localization-package-boundaries.md |
For a quick task (small widget fix, one-line review comment), the core rules
below may be enough on their own.
Core rules (always apply)
Architecture
- Separate UI from data. Two broad layers: UI (views + view models /
cubits) and Data (repositories + services). Dependencies point one way:
View → ViewModel → Repository → Service. Lower layers never import upper
layers. Repositories never depend on each other.
- Views hold no business logic. Widgets may contain show/hide
conditionals, animation, layout, and simple routing logic — nothing that
transforms or decides about data. All data logic lives in the view model
(or cubit/bloc), which has no access to
BuildContext.
- Organize by feature, not by layer. Everything a feature needs — state
management, widgets, models — lives under one feature directory. Don't
create top-level
blocs/, widgets/, models/ buckets that scatter a
feature across the tree.
- State is immutable and explicit. Model UI state as a sealed/union type
(initial / inProgress / failure / ready) so every case is handled
exhaustively. One-off effects (snackbars, navigation) are events, not
state.
- Add layers only when they pay for themselves. Start with
view-model → API client. Introduce a repository when you need caching,
offline, or merging sources. Introduce a use case only when logic merges
multiple repositories, is genuinely complex, or is reused by several view
models.
Coding
- Prefer intent-revealing widgets over
Container. Use Padding,
SizedBox, ColoredBox, DecoratedBox, Center — they are const-able
and self-describing. Container is fine only when combining several
properties at once.
- Use modern Dart. Pattern matching (
if (x case final v?)), switch
expressions with exhaustiveness, records and destructuring, collection
if/for/spreads instead of .add() loops, expression bodies for
pass-through async functions (no redundant async/await).
- Prefix sliver-returning widgets with
Sliver so misuse in the wrong
scroll context is caught at a glance.
- Tests tell a story. Use expressive matchers (
isEmpty, throwsA,
isA, completion) and minimize dependencies — plain Text/SizedBox
over design-system widgets in test fixtures. Test cubits/view models in
isolation from the widget tree.
- Every
// ignore: gets a reason on the same or preceding line. Log
errors with dedicated error/stackTrace parameters, never string
interpolation.
Workflow checklists
Adding a new feature/screen
- Read references/feature-structure.md
and mirror the existing project's conventions for the feature directory.
- Define the state as a union type first; then the cubit/view model; then
the widgets. Constructor-inject dependencies; scope them to the feature's
widget subtree.
- Data comes in through a repository or typed API client — never fetched
inside a widget.
- Add unit tests for the cubit/view model logic before wiring UI details.
Reviewing Flutter code
Check, in order of importance:
- Logic in the right layer (rule 1–2)? Any
BuildContext in business logic?
- State modeled as immutable union types, all cases handled?
- Feature self-contained, or does it reach into another feature's internals?
- Widget choices (rule 6), modern Dart (rule 7), sliver naming (rule 8)?
- Tests present for logic, readable, minimal dependencies?
- Unexplained
// ignore:, string-interpolated error logs, deprecated
patterns still spreading?
Starting a new project
- Read references/architecture-layers.md
for the layer blueprint and
references/feature-structure.md for the
folder skeleton.
- If more than ~2 teams or clearly separable domains are involved, read
references/enterprise-scale.md and
consider a Melos monorepo with one package per domain from day one.
- Set up strict lints early (
leancode_lint or equivalent + custom rules) —
retrofitting is far more expensive.
Package palette
Defaults that these standards assume (swap for project-local equivalents when
the codebase already uses something else):
- State:
bloc (Cubit) + freezed for union-type states;
bloc_presentation for one-off UI events
- DI:
provider scoped to widget subtrees (accepting its lack of
compile-time safety as the lesser evil)
- Boilerplate reduction:
flutter_hooks
- Monorepo:
melos
- Localization:
flutter_localizations + intl with .arb files
- Lints:
leancode_lint
- E2E/UI tests:
patrol
The official Flutter guide is state-management-agnostic (MVVM with
ChangeNotifier works too); what matters is the layer separation, not the
package. See the reference files for rationale and trade-offs.
1---2name: flutter-best-practices3description: Use when writing, reviewing, refactoring, or planning Flutter/Dart code — screens, features, project structure, state management, folders, widgets, cubits/blocs, repositories, services, or tests.4---5
6# Flutter Best Practices
7
8Standards for building maintainable Flutter apps, distilled from the official
9Flutter architecture guide and LeanCode's experience shipping 40+ Flutter
10projects (including a 30-developer banking app). Apply these when writing new
11code; when touching existing code, prefer consistency with the surrounding
12codebase and raise conflicts with these standards rather than silently
13rewriting.
14
15## How to use this skill
16
17Read the reference file that matches the task. Read more than one when tasks
18overlap (e.g. a new feature touches both structure and coding style).
19
20| Task | Read |
21|---|---|
22| Design layers, decide where logic lives, MVVM, repositories/services | [references/architecture-layers.md](references/architecture-layers.md) |
23| Create/organize a feature, folder structure, state management wiring | [references/feature-structure.md](references/feature-structure.md) |
24| Write or review Dart/Flutter code, widgets, tests, lints | [references/dart-coding-practices.md](references/dart-coding-practices.md) |
25| Multi-team/multi-package apps, monorepo, navigation, localization, API contracts, E2E tests | [references/enterprise-scale.md](references/enterprise-scale.md) |
26| App localization setup, reusable UI package string ownership, language picker visibility | [references/localization-package-boundaries.md](references/localization-package-boundaries.md) |
27
28For a quick task (small widget fix, one-line review comment), the core rules
29below may be enough on their own.
30
31## Core rules (always apply)
32
33### Architecture
34
351. **Separate UI from data.** Two broad layers: UI (views + view models /
36 cubits) and Data (repositories + services). Dependencies point one way:
37 `View → ViewModel → Repository → Service`. Lower layers never import upper
38 layers. Repositories never depend on each other.
392. **Views hold no business logic.** Widgets may contain show/hide
40 conditionals, animation, layout, and simple routing logic — nothing that
41 transforms or decides about data. All data logic lives in the view model
42 (or cubit/bloc), which has no access to `BuildContext`.
433. **Organize by feature, not by layer.** Everything a feature needs — state
44 management, widgets, models — lives under one feature directory. Don't
45 create top-level `blocs/`, `widgets/`, `models/` buckets that scatter a
46 feature across the tree.
474. **State is immutable and explicit.** Model UI state as a sealed/union type
48 (initial / inProgress / failure / ready) so every case is handled
49 exhaustively. One-off effects (snackbars, navigation) are events, not
50 state.
515. **Add layers only when they pay for themselves.** Start with
52 view-model → API client. Introduce a repository when you need caching,
53 offline, or merging sources. Introduce a use case only when logic merges
54 multiple repositories, is genuinely complex, or is reused by several view
55 models.
56
57### Coding
58
596. **Prefer intent-revealing widgets over `Container`.** Use `Padding`,
60 `SizedBox`, `ColoredBox`, `DecoratedBox`, `Center` — they are const-able
61 and self-describing. `Container` is fine only when combining several
62 properties at once.
637. **Use modern Dart.** Pattern matching (`if (x case final v?)`), switch
64 expressions with exhaustiveness, records and destructuring, collection
65 `if`/`for`/spreads instead of `.add()` loops, expression bodies for
66 pass-through async functions (no redundant `async`/`await`).
678. **Prefix sliver-returning widgets with `Sliver`** so misuse in the wrong
68 scroll context is caught at a glance.
699. **Tests tell a story.** Use expressive matchers (`isEmpty`, `throwsA`,
70 `isA`, `completion`) and minimize dependencies — plain `Text`/`SizedBox`
71 over design-system widgets in test fixtures. Test cubits/view models in
72 isolation from the widget tree.
7310. **Every `// ignore:` gets a reason** on the same or preceding line. Log
74 errors with dedicated `error`/`stackTrace` parameters, never string
75 interpolation.
76
77## Workflow checklists
78
79### Adding a new feature/screen
80
811. Read [references/feature-structure.md](references/feature-structure.md)
82 and mirror the existing project's conventions for the feature directory.
832. Define the state as a union type first; then the cubit/view model; then
84 the widgets. Constructor-inject dependencies; scope them to the feature's
85 widget subtree.
863. Data comes in through a repository or typed API client — never fetched
87 inside a widget.
884. Add unit tests for the cubit/view model logic before wiring UI details.
89
90### Reviewing Flutter code
91
92Check, in order of importance:
93
941. Logic in the right layer (rule 1–2)? Any `BuildContext` in business logic?
952. State modeled as immutable union types, all cases handled?
963. Feature self-contained, or does it reach into another feature's internals?
974. Widget choices (rule 6), modern Dart (rule 7), sliver naming (rule 8)?
985. Tests present for logic, readable, minimal dependencies?
996. Unexplained `// ignore:`, string-interpolated error logs, deprecated
100 patterns still spreading?
101
102### Starting a new project
103
1041. Read [references/architecture-layers.md](references/architecture-layers.md)
105 for the layer blueprint and
106 [references/feature-structure.md](references/feature-structure.md) for the
107 folder skeleton.
1082. If more than ~2 teams or clearly separable domains are involved, read
109 [references/enterprise-scale.md](references/enterprise-scale.md) and
110 consider a Melos monorepo with one package per domain from day one.
1113. Set up strict lints early (`leancode_lint` or equivalent + custom rules) —
112 retrofitting is far more expensive.
113
114## Package palette
115
116Defaults that these standards assume (swap for project-local equivalents when
117the codebase already uses something else):
118
119- **State:** `bloc` (Cubit) + `freezed` for union-type states;
120 `bloc_presentation` for one-off UI events
121- **DI:** `provider` scoped to widget subtrees (accepting its lack of
122 compile-time safety as the lesser evil)
123- **Boilerplate reduction:** `flutter_hooks`
124- **Monorepo:** `melos`
125- **Localization:** `flutter_localizations` + `intl` with `.arb` files
126- **Lints:** `leancode_lint`
127- **E2E/UI tests:** `patrol`
128
129The official Flutter guide is state-management-agnostic (MVVM with
130ChangeNotifier works too); what matters is the layer separation, not the
131package. See the reference files for rationale and trade-offs.