Sub topic 8
Exposing a Feature Through One Public File
This chapter assumes you can build Flutter widgets, use Dart imports and export, navigate between screens, and understand the repository pattern. The goal is to make a feature easy to use without exposing its internal structure.
Why a public entry file matters
A feature usually contains presentation widgets, state, domain models, repositories, data sources, and routes. Other parts of the app should not need to know where each file lives.
A public entry file, often called a barrel file, lists the parts of a feature that other code may use. For the cart feature, this file might be lib/features/cart/cart.dart.
// lib/features/cart/cart.dart
// Public API of the cart feature.
export 'cart_routes.dart';
export 'domain/cart_item.dart';
export 'domain/cart_repository.dart';Code outside the feature imports this one file:
import '../features/cart/cart.dart';
void useCart(CartRepository repository) {
// The caller can use the repository contract.
}This creates a stable import path. You can move CartItem from domain/cart_item.dart to another internal location later without changing every caller. Only the export in cart.dart needs to change.
Choosing what to export
Export types that represent a feature's supported capabilities. Keep implementation details inside the feature.
Usually export:
- Domain models that callers must display or pass around.
- Repository interfaces that callers must provide or use.
- Route definitions that the app shell must register.
- Small contracts designed for cross-feature communication.
Usually keep private:
- Page widgets used only by the feature's routes.
- Concrete repository implementations.
- API clients and database data sources.
- Feature-specific state classes.
- Mapping and validation helpers.
The barrel file is a boundary, but Dart does not prevent someone from importing an internal path directly. The team must agree that direct imports into another feature's folders are forbidden. Static analysis or import-lint rules can enforce that agreement.
Keeping the API small
A smaller API gives a feature fewer promises to maintain. If a widget is needed only by the cart feature, do not export it merely because another file could technically import it.
Prefer an interface over a concrete implementation:
// lib/features/cart/domain/cart_repository.dart
import 'cart_item.dart';
abstract interface class CartRepository {
Future<List<CartItem>> loadItems();
Future<void> addItem(CartItem item);
}The checkout feature can depend on this contract without depending on the cart's storage system. The cart can later replace an in-memory repository with a remote implementation without changing checkout.
Using the public API for routing
The app shell can register routes through the feature's entry file:
// lib/app/router.dart
import 'package:go_router/go_router.dart';
import '../features/cart/cart.dart';
import '../features/catalog/catalog.dart';
final router = GoRouter(
routes: [
...catalogRoutes,
...cartRoutes,
],
);The app knows that the cart provides routes, but it does not need to import presentation/cart_page.dart. This keeps route ownership inside the feature.
Practical rules
Use these rules when reviewing imports:
- Import another feature only through its public entry file.
- Do not export every file in a feature.
- Export interfaces before concrete implementations.
- Keep route registration in the feature that owns the route.
- Change the public file when the feature's supported capabilities change.
- Treat a direct internal import as a boundary violation.
Exercise
The checkout feature needs to read the number of items in the cart. Should it import the cart page, a concrete cart repository, or a small public contract?
Show answer
It should depend on a small public contract, such as a CartSummary interface, exported by the cart feature or defined in shared contracts.