Structuring a Publishable Library
Lay out lib, src, and the public API.
Structuring a Publishable Library is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Package Is Just a Folder
A publishable Dart package is simply a folder with a pubspec.yaml at its root. That one file turns plain code into something pub.dev can host. 📦
The lib Folder Is the Public Home
Everything you want users to import lives under lib. Files directly in lib are part of your package's public surface, so place them with care.
Hide Internals in lib/src
Code you do not want exposed goes in lib/src. By convention these files are private implementation details, not meant for direct import by users.
lib/
my_package.dart
src/
helper.dartThe Entry Point File
Most packages ship one main file in lib named after the package. This entry point is what users import to get your whole library at once.
import 'package:my_package/my_package.dart';Re-export With the export Keyword
Your entry point uses export to surface the pieces from lib/src that users actually need. You curate one clean, intentional public API.
export 'src/calculator.dart';
export 'src/parser.dart';Decide Your Public API Deliberately
The set of names you export is your public API. Keep it small and stable, because every exported symbol is a promise to your users.
Underscores Mean Private
Names starting with an underscore are library-private in Dart. Even inside lib/src, prefix helpers you never want leaked with one underscore.
int _internalCount = 0;The example Folder
Add an example folder with a small runnable program. It shows users how your package works and earns you points on pub.dev. ✨
Tests Belong in test
Automated tests live in the test folder, outside lib. They are not shipped to users but prove your package behaves as promised.
Top-Level Files Matter
A good package root also carries a README, a CHANGELOG, and a LICENSE. These three files are the first thing pub.dev shows visitors.
A Clean Layout at a Glance
Put it together and the structure reads like a map: lib for the API, src for internals, plus example, test, and the metadata files.
my_package/
lib/
example/
test/
pubspec.yamlQuick Check
Where do you place code that should stay an internal implementation detail?
Recap: A Tidy Library
You learned the layout: lib holds the public API, lib/src hides internals, and export curates what users see. Structure first, then ship. 🚀
Frequently asked questions
Is the “Structuring a Publishable Library” lesson free?
Yes — the full text of “Structuring a Publishable Library” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Structuring a Publishable Library”?
Lay out lib, src, and the public API. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Structuring a Publishable Library” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Structuring a Publishable Library
- Documenting With dartdoc Comments
- Linting, Formatting, and pana Score
- dart pub publish to pub.dev