Sub topic 8
Exposing a Feature Through One Public File
This chapter assumes you know Dart imports, libraries, and the export directive. It also assumes you can read a feature folder with data, domain, and presentation parts, and that you know the dependency direction rules from the previous chapter. Here you will learn how a single entry file, called a barrel file, turns a folder of Dart files into a feature with a defined public surface.
The problem: Dart has no folder privacy
Dart's privacy unit is the library, which is usually one file. A name that starts with _ is private to its library. There is no keyword that means "visible inside this folder but not outside it." Every public class in lib/features/cart/data/ can be imported by any other file in the app.
Without a rule, other features will import whatever is closest to hand:
import '../../cart/data/in_memory_cart_repository.dart';This couples the importing feature to one implementation file. If you rename or split that file, or replace the in-memory repository with a network one, the other feature breaks. The folder structure suggested a boundary, but nothing enforced it.
The usual answer is a convention: each feature has one file that lists what it offers, and other features import only that file.
The barrel file
A barrel file is a Dart file that contains only export directives. It re-exports selected names from files inside the feature. By convention it sits at the feature root and carries the feature's name.
// lib/features/cart/cart.dart
// Public API of the cart feature. Other features import only this file.
export 'cart_routes.dart';
export 'domain/cart_item.dart';
export 'domain/cart_repository.dart';A consumer now writes:
import 'package:shop/features/cart/cart.dart';Here shop stands for your app's package name from pubspec.yaml. The consumer sees cartRoutes, CartItem, and CartRepository. It does not see InMemoryCartRepository or CartPage, because the barrel does not export them. Those files remain technically importable, but importing them directly is now a visible violation of the convention, and the next chapters show how to catch it automatically.
What the barrel is for
The barrel does two jobs.
- It documents the feature's contract. Reading one short file tells you what the feature offers.
- It gives you freedom inside the feature. You can move, rename, or split any file that is not exported, and no code outside the feature changes.
The diagram below shows the shape of this boundary.
Deciding what to export
Export the smallest set that other code legitimately needs. A useful way to decide is to ask what each part is for.
| Part | Export? | Reason |
|---|---|---|
| Route list (`cartRoutes`) | Yes | The app shell must register it. |
| Domain entities (`CartItem`) | Usually | Other features may need to name the type. |
| Repository interface (`CartRepository`) | Usually | Lets the shell wire an implementation and lets others depend on the abstraction. |
| Repository implementation | No | Implementation detail, free to change. |
| Pages and widgets | Rarely | Reach them through routes. Export a widget only if it is designed for embedding, such as a cart badge. |
| State holders (controllers, notifiers) | Rarely | Export only if another part of the app must read or override them. |
| DTOs and data sources | No | They belong to the data layer. |
If you find yourself exporting most of the feature, the feature may be too tightly bound to its consumers. Consider the contract pattern covered in the cross-feature chapter instead.
Selective exports with `show` and `hide`
A barrel can export a whole file, or only some of its names.
// lib/features/cart/cart.dart
export 'domain/cart_item.dart' show CartItem;
export 'domain/cart_repository.dart';With show, only CartItem leaves the file, even if cart_item.dart also declares helper classes. hide does the opposite and lists names to withhold. Prefer show for a public API, because a new class added to the file later stays private by default. With hide, a new class is exported until someone remembers to hide it.
Inside the feature, import directly
The barrel is for code outside the feature. Files inside the feature should import each other directly, with relative paths, as the repository example does:
// lib/features/cart/data/in_memory_cart_repository.dart
import '../domain/cart_item.dart';
import '../domain/cart_repository.dart';
class InMemoryCartRepository implements CartRepository {
final List<CartItem> _items = [];
@override
Future<List<CartItem>> loadItems() async => List.unmodifiable(_items);
@override
Future<void> addItem(CartItem item) async {
_items.add(item);
}
}// lib/features/cart/domain/cart_repository.dart
import 'cart_item.dart';
abstract interface class CartRepository {
Future<List<CartItem>> loadItems();
Future<void> addItem(CartItem item);
}// lib/features/cart/domain/cart_item.dart
class CartItem {
const CartItem({required this.id, required this.name, required this.quantity});
final String id;
final String name;
final int quantity;
}Common mistakes
Exporting everything
A barrel that exports every file in the feature has the same effect as no barrel. Other features will depend on pages, controllers, and data classes, and you lose the freedom to change them. Review the barrel in code review as you would review a public API.
Exporting implementations to make wiring easy
The app shell needs to construct an InMemoryCartRepository at startup, which tempts you to export it. Resist this where you can. Two alternatives keep the implementation hidden:
- Export a small factory function from the barrel, such as
CartRepository createCartRepository(), defined in a file in the feature. - Export a provider or registration function that the shell calls, so the shell never names the concrete class.
The first option looks like this:
// lib/features/cart/data/cart_repository_factory.dart
import '../domain/cart_repository.dart';
import 'in_memory_cart_repository.dart';
CartRepository createCartRepository() => InMemoryCartRepository();// lib/features/cart/cart.dart
export 'cart_routes.dart';
export 'data/cart_repository_factory.dart';
export 'domain/cart_item.dart';
export 'domain/cart_repository.dart';The shell calls createCartRepository() and receives a CartRepository. Swapping the implementation later changes one file inside the feature.
Leaking types through signatures
A name you do not export can still reach consumers if an exported signature uses it. If an exported function returns a CartDto that the barrel does not export, callers can use the value but cannot name its type. The analyzer may also warn about this in some configurations. Keep exported signatures limited to exported types, usually domain types.
Wide barrels in the shared layer
Avoid one giant core.dart that re-exports everything in core. Any import of it pulls the whole shared layer into the dependency picture and hides which shared pieces a feature really uses. Prefer small files, such as core/contracts/cart_summary.dart, imported by path.
Name collisions
If two barrels export a class with the same name, a file importing both gets an ambiguity error on use. Fix it with a prefix (import '...' as cart;) or, better, rename one class so the names are distinct across features.
Barrels and the rest of the structure
The barrel is also where a feature's routes become visible to the app shell. The shell imports the barrels and combines the route lists:
// 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 shell depends on cart.dart and catalog.dart only. It never reaches into presentation. The same pattern supports the later chapters on routing, cross-feature communication, and splitting features into packages. A package has a natural public file, usually lib/cart.dart, and everything under lib/src/ is private by Dart convention. A well-kept barrel makes that move almost mechanical.
Checklist for a barrel file
- It lives at the feature root and is named after the feature.
- It contains only
exportlines and comments. - It uses
showfor files that declare more than you intend to expose. - Every exported signature uses only exported types.
- No file inside the feature imports it.
- Other features import it and nothing deeper.
Exercise
The checkout feature imports package:shop/features/cart/data/in_memory_cart_repository.dart to read the cart. Name two problems with this and describe the fix.
Show answer
First, checkout now depends on a concrete implementation, so replacing or renaming it in the cart feature breaks checkout. Second, it bypasses the cart feature's public API, so the boundary is no longer meaningful. The fix is to have checkout import only cart.dart and depend on the CartRepository interface, or better, on a small shared contract such as CartSummary, with the app shell supplying the implementation.
Exercise
A barrel contains export 'domain/cart_item.dart';. Later someone adds a helper class CartItemDebugPrinter to that file. What happens to the barrel's public surface, and how would you prevent it?
Show answer
CartItemDebugPrinter becomes public to every consumer, because a plain export re-exports every public name in the file. Change the line to export 'domain/cart_item.dart' show CartItem; so only the named type is exposed, or move the helper to a separate file that the barrel does not export.
Exercise
Why should presentation/cart_page.dart not import cart.dart, the feature's own barrel?
Show answer
The barrel exports cart_routes.dart, which imports cart_page.dart, so the import would form a cycle. Files inside a feature should import each other with direct relative paths and keep the barrel for outside code.