Sub topic 10
When One Feature Needs Another
Assume you know Flutter widgets and navigation, Dart imports and export, one state management approach, and the repository pattern. This chapter focuses on communication between feature folders without coupling them to each other’s private code.
A feature may need information from another feature. For example, checkout needs the cart’s item count, but checkout should not import the cart’s database code, widgets, or state controller. A direct import creates a dependency on implementation details.
Depend on a small contract
A contract is an interface that describes what another feature can provide. Put the contract in shared code when several features need it. The consuming feature depends on the contract, not on the producing feature.
abstract interface class CartSummary {
int get itemCount;
bool get isEmpty;
}
class CheckoutController {
CheckoutController(this.cart);
final CartSummary cart;
String startCheckout() {
if (cart.isEmpty) {
return 'Add an item before checkout';
}
return 'Checkout started for ${cart.itemCount} item(s)';
}
}
class CurrentCart implements CartSummary {
CurrentCart(this.itemCount);
@override
final int itemCount;
@override
bool get isEmpty => itemCount == 0;
}
void main() {
final cart = CurrentCart(2);
final checkout = CheckoutController(cart);
print(checkout.startCheckout());
}CheckoutController knows only the CartSummary contract. The app shell can provide CurrentCart, a mock, or a state-management adapter. Checkout does not need to know where the data came from.
A contract should contain the smallest useful surface. If checkout only needs an item count, do not expose the entire cart repository or every cart command.
Choose a communication direction
Use these patterns for common cases:
- Pass a value or contract into a page when the relationship is local and explicit.
- Use a shared read-only contract when several features need the same current state.
- Return a result from a route when one feature opens another and waits for a decision.
- Use an application-level event or command for a meaningful action that does not belong to the receiving feature.
- Move truly reusable business rules into
coreor a shared package.
Avoid importing another feature’s data, domain, or presentation folders directly. Those imports bypass the public API and make future changes expensive.
Keep ownership clear
The feature that owns a piece of state should update it. Other features may request a change through a public method or command, but they should not mutate the owner’s private state.
The app shell is the composition root. It creates concrete implementations and supplies them to features. This keeps feature internals independent while making the wiring visible in one place.
Common errors
A common error is importing cart/presentation/cart_page.dart from checkout. A page is a user interface, not a stable integration boundary. Another error is creating a shared “god service” that exposes every feature. That replaces many direct dependencies with one difficult-to-test dependency.
Watch for cycles such as catalog importing cart while cart imports catalog. Break the cycle with a shared contract, a route result, or an app-level coordinator. If two features share a large amount of behavior, reconsider whether they are separate features or whether the shared behavior belongs in a smaller domain package.
Exercise
Checkout needs to display the cart item count and request payment. Should it import the cart repository?
Show answer
No. Define a small shared read contract for the count and a public command or application service for starting payment. Let the app shell connect those contracts to the cart implementation.