Imports, Exports, and Libraries
Organize and re-export your code.
Imports, Exports, and Libraries is a free Dart Academy lesson on CoddyKit — lesson 3 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.
What an Import Does
An import pulls another file or package into your code so you can use its classes and functions. Every Dart file starts with them. 📥
import 'dart:math';Importing the Core SDK
Built-in libraries use the dart: prefix. For example, dart:math gives you sqrt and Random without installing anything.
import 'dart:convert';Importing a Package
To use a dependency from pub, import it with the package: prefix followed by the package name and the file path inside it.
import 'package:http/http.dart';Importing Your Own Files
For files in your own project, use a relative path in quotes. This keeps related code linked without a package prefix.
import 'models/user.dart';Prefixing an Import
If two libraries share a name, add an as prefix. Then you call members through that prefix to avoid any clash.
import 'package:http/http.dart' as http;Showing Only What You Need
Use show to import just specific names from a library. This keeps your namespace clean and your intent obvious.
import 'dart:math' show pi, sqrt;Hiding Unwanted Names
The opposite of show is hide. It imports everything except the names you list, which is great for dodging a single conflict.
import 'dart:math' hide Random;What a Library Is
In Dart, every file is a library by default. Public top-level members are visible to importers; nothing extra is required.
Privacy With Underscore
A name that begins with an underscore is private to its library. Importers cannot see it, which is how you hide implementation details.
int _secret = 42;Re-Exporting With export
The export directive forwards another library's public names through your file, so users import one entry point instead of many.
export 'src/user.dart';Building a Barrel File
A barrel file is just a library full of exports. It gives your package a single clean public API surface for everyone to import.
export 'src/user.dart';
export 'src/order.dart';Quick Check
How do you make a top-level name private to its library?
Recap
You learned to import SDK, package, and local code, refine it with show and hide, and expose a clean API using export. 🎉
Frequently asked questions
Is the “Imports, Exports, and Libraries” lesson free?
Yes — the full text of “Imports, Exports, and Libraries” 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 “Imports, Exports, and Libraries”?
Organize and re-export your code. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Imports, Exports, and Libraries” 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
- Anatomy of pubspec.yaml
- Adding Dependencies With dart pub
- Imports, Exports, and Libraries
- Version Constraints and the Lockfile