Lesson 3 of 9 · 27m
Project Shape, Dependency Discipline, and Tests That Catch Production Bugs
Organize a Flutter codebase around ownership boundaries, select dependencies conservatively, and invest in tests at the layers where regressions actually occur.
The architecture conversation usually starts too late, after three state packages, five model formats, and two networking clients have already fused into the same feature folder. You do not need enterprise theater. You do need a shape that lets a new hire answer: where does this rule live, who owns this object, and what can I safely change?
A boring structure wins
For production applications, a feature-first structure with explicit layers is usually enough:
lib/
main.dart
app/
router.dart
theme.dart
bootstrap.dart
core/
clock.dart
errors.dart
logging.dart
features/
billing/
data/
domain/
presentation/
devices/
data/
domain/
presentation/data translates transport and persistence. domain owns business rules and immutable models. presentation owns widgets and view state. This is not purity for its own sake; it lets tests hit the rules without booting a phone.
Dependencies are operational liabilities
Every package is code you will maintain after the author moves on. Before adding one, inspect maintenance cadence, native code surface, transitive dependencies, platform support, privacy implications, and migration cost. Pin versions through pubspec.lock, review diffs on upgrade, and separate direct intent from transitive accident:
dependencies:
flutter:
sdk: flutter
dio: ^5.7.0
riverpod: ^2.5.1
shared_preferences: ^2.3.2
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
mocktail: ^1.0.4A package that solves ten lines of work but introduces a native build failure is not convenience. It is outsourcing your release risk.
Test at the seams, not just the widgets
A useful test pyramid for Flutter is shallow but broad:
unit tests domain rules, serializers, reducers, adapters
widget tests layout state, interaction, accessibility semantics
integration tests auth flow, storage migration, one critical purchase or sync path
golden tests small design-system components, not every screenGolden tests are brittle across fonts and platforms. Use them for controlled components. Widget tests should verify behavior users depend on, not private implementation details. A test that breaks because you renamed a private variable is friction; a test that breaks because offline retry stopped working is value.
Make failure reproducible
Log structured application events with correlation IDs, but treat logs as sensitive. A well-shaped error carries kind, operation, retryability, and safe metadata:
sealed class AppError implements Exception {
const AppError(this.operation);
final String operation;
}
final class OfflineError extends AppError {
const OfflineError(super.operation);
}Sealed classes make exhaustive handling possible. That matters when a product manager asks why payment errors and offline errors show the same banner.
Field notes
- Put generated files behind explicit generation commands in CI; do not let local tool drift decide what compiles.
- Avoid circular feature imports. If two features need the same rule, move the rule down to
coreor a shared domain package. shared_preferencesis not a database and not a secret store; it is for small non-sensitive preferences.- Run
flutter analyzewith fatal infos in CI after the codebase is clean enough to keep it green. - If tests require a real backend, they are integration tests. Label and isolate them so ordinary contributors can still run fast suites.
Was this lesson helpful?