Sub topic 14
Moving Features into Separate Packages
When a feature deserves a package
A feature package is a Dart package that contains one feature and its internal code. The app imports the package through its public API instead of importing files from the feature's folder.
Assume you already know Flutter widgets and navigation, Dart imports and exports, one state-management approach, and the repository pattern. This chapter focuses on the boundary between packages, not on package basics.
A package is useful when a feature has:
- A clear public API.
- Few dependencies on other features.
- Its own tests and release needs.
- Enough code that the main application package is difficult to navigate.
- A possible future life in another application.
A package adds setup and dependency management. Do not split every small feature immediately. A package boundary is worthwhile when it improves ownership or prevents unwanted dependencies.
A feature package layout
A local package can live beside the application:
packages/
cart/
lib/
cart.dart
src/
data/
domain/
presentation/
cart_routes.dart
test/
pubspec.yaml
app/
lib/
app/
router.dart
main.dart
pubspec.yamlThe src directory is private by convention. The cart.dart file is the package's public entry point:
// packages/cart/lib/cart.dart
export 'src/cart_routes.dart';
export 'src/domain/cart_item.dart';
export 'src/domain/cart_repository.dart';Code outside the package should import package:cart/cart.dart, not a file under cart/src.
# app/pubspec.yaml
name: shop_app
dependencies:
flutter:
sdk: flutter
cart:
path: ../packages/cartA path dependency lets the app use a local package during development. Later, the same package can be published or referenced from a Git repository.
Dependency direction and package APIs
The feature package may depend on shared packages, Flutter, and selected infrastructure packages. It should not depend on the application package. The application composes packages; packages should not reach upward into the application.
Keep cross-feature dependencies narrow. For example, checkout should depend on a shared CartSummary interface rather than importing cart's data source or state controller. If two feature packages depend on each other, move the shared contract into a small package or let the app provide the connection.
| Boundary | Allowed dependency | Avoid |
|---|---|---|
| App to feature | Public package API | Feature internals |
| Feature to shared code | Stable shared contracts and utilities | App-specific widgets |
| Feature to feature | Small shared contract | Direct state or data imports |
Moving an existing feature
Migrate one feature at a time:
- List the feature's imports and identify app-specific dependencies.
- Create the package with its own
pubspec.yaml,lib, andtestdirectories. - Move the feature's code into
lib/src. - Create the single public entry file.
- Replace imports of application internals with shared contracts or constructor parameters.
- Add the package as a path dependency.
- Update the app's routes and tests to use the public API.
- Remove the old copy only after tests and analysis pass.
Keep the package's public surface small. If callers need many internal classes, the feature may expose the wrong abstraction or the migration may have started too early.
Exercise
The cart package needs the app's authentication state. Should it import the app package?
Show answer
No. Define a small authentication interface in shared code, pass an implementation into the cart, or let the app coordinate the operation. The cart package must not depend on its composing application.