Sub topic 4
Deciding What Is Shared
Assume you already know Flutter widgets, Dart imports and exports, navigation, and one state-management approach. This chapter focuses on deciding whether code belongs in a feature or in shared code.
Shared code has a cost
Shared code is available to multiple features. Typical examples include app themes, network clients, logging, common buttons, and authentication contracts.
Sharing code avoids duplication, but it also creates dependencies. When many features depend on one shared class, changing that class can affect the whole application. A large core folder can become a second miscellaneous folder if every reusable-looking class is placed there.
Use this test before moving code into shared:
- Does more than one feature need it now?
- Does it have the same meaning in every feature?
- Can it remain independent of a specific business feature?
- Would sharing reduce duplication without hiding important differences?
If the answer is no, keep the code inside its feature. You can move it later when a real second use appears.
Feature code versus shared code
A product concept belongs in a feature even when several screens use it. For example, cart pricing rules belong in features/cart, not in core, because the rules describe the cart domain.
A technical service can belong in shared code when it has no product-specific meaning. An HTTP client, a date formatter, or an application-wide error type may belong in core.
| Code | Likely location | Reason |
|---|---|---|
| Cart total calculation | `features/cart/domain` | It expresses cart behavior |
| HTTP client | `core/network` | Many features can use the same transport |
| Product card | `features/catalog/presentation` | Its layout represents catalog behavior |
| App theme | `core/theme` | It defines application-wide styling |
Use contracts to avoid feature imports
Sometimes one feature needs a small piece of another feature. Do not solve this by importing the other feature’s internal widgets or state classes. Define a narrow contract in shared code instead.
abstract interface class CartSummary {
int get itemCount;
}
class CartBadgeText {
const CartBadgeText(this.cart);
final CartSummary cart;
String text() => 'Cart items: ${cart.itemCount}';
}
class DemoCart implements CartSummary {
const DemoCart(this.itemCount);
@override
final int itemCount;
}
void main() {
const cart = DemoCart(3);
print(CartBadgeText(cart).text());
}The checkout feature can depend on CartSummary without depending on cart storage, screens, or state management. The app shell supplies an implementation when it composes the features.
Keep dependency direction clear
A useful direction is:
Shared code should not import feature internals. If core needs to know about a specific feature, the dependency is probably reversed. Put the abstraction in core, then let the app shell connect it to a feature implementation.
A practical decision process
When reviewing a class, ask:
- What business concept does this class describe?
- Which feature owns that concept?
- Is the class reused, or only planned for reuse?
- Does its API expose feature-specific terms?
- Would a small interface remove the need for a feature-to-feature import?
Keep the first version local. Extract only after duplication or a genuine cross-feature contract appears.
Exercise
Where should a PaymentReceipt model live if only the checkout feature uses it, even though two checkout screens display it?
Show answer
It should remain inside features/checkout, because both screens use it within the same business feature. Move it to shared code only when another feature needs the same concept.