Sub topic 15
Migrating an Existing App One Feature at a Time
Assume you can build Flutter widgets, use Dart imports and exports, navigate between screens, and recognize the repository pattern. This chapter focuses on changing an existing layer-first project without stopping feature development.
Start with a boundary, not a rewrite
A layer-first project often looks like this:
lib/
screens/
models/
repositories/
services/
widgets/This arrangement is easy to start, but related files become scattered. A cart change may require searching several large folders. Moving everything at once creates merge conflicts and makes regressions difficult to locate.
Use an incremental migration instead:
- Choose one feature with a clear boundary.
- Inventory its screens, models, state, repositories, and tests.
- Create the new feature folder.
- Move or copy the feature’s code.
- Update imports and tests.
- Add a public entry file.
- Remove the old files only after the application works.
- Repeat for the next feature.
The first feature should be important enough to reveal problems, but small enough to migrate safely. A cart, settings, or catalog feature is often a better starting point than authentication or the app shell.
Keep compatibility while moving
During migration, old code may still depend on a class that is moving. Preserve a temporary forwarding file rather than changing every caller in one commit.
For example, after moving CartItem to the cart feature, the old model file can re-export it:
// lib/models/cart_item.dart
export '../features/cart/domain/cart_item.dart';Existing imports continue to compile. New code imports the feature’s public file instead. Delete the forwarding file after all callers have migrated.
A forwarding file is a transition tool, not a permanent second public API. Track these files in an issue or checklist so they do not remain unnoticed.
Migrate in dependency order
Move inward-facing code before code that uses it:
- Domain types and repository interfaces.
- Data sources and repository implementations.
- Feature state and controllers.
- Pages and widgets.
- Routes and app-level composition.
- Tests that belong to the feature.
If a screen imports a global service directly, do not hide that dependency during the move. Either move the service into the feature or introduce a small shared contract. A feature should depend on stable abstractions, not on another feature’s internal files.
Verify each step
After moving a slice, run formatting, static analysis, unit tests, widget tests, and the relevant integration flow. Search for imports that still point to the old folders. Also inspect dependency direction: presentation may use domain contracts, and data may implement domain contracts, but unrelated features should not import private implementation files.
The app shell can temporarily compose both migrated and unmigrated features. Do not force all features to adopt the new structure before the first migration proves useful.
Exercise
Exercise
You moved a feature’s page but its tests still import the old repository path. What is the safest next step?
Show answer
Add a temporary forwarding export or update the tests in the same small migration, then run the feature’s tests before deleting the old path.