Sub topic 1
What pub.dev is and how packages and plugins differ
Prerequisites
This chapter assumes you can do the following:
- Read basic Dart syntax: imports, functions,
asyncandawait. - Create a Flutter project with
flutter createand run it. - Run commands in a terminal.
- Read a short YAML file (indentation, key-value pairs, lists).
You do not need to have added a package before. This chapter covers the ecosystem and the vocabulary. The chapters that follow cover the commands.
What pub.dev is
pub.dev is the official package repository for Dart and Flutter. A package is a unit of reusable code that someone published so other projects can depend on it. Instead of writing an HTTP client, a JSON serializer, or a state management library yourself, you declare a dependency on a published package and the Dart tooling downloads it for you.
The repository is run by the Dart team, but most packages come from outside Google. Anyone with a Google account can publish. This is why evaluating a package before you adopt it matters, and a later chapter covers how.
Three pieces work together:
- pub.dev is the website and the server that hosts package archives and metadata.
- The pub tool is the command-line client. You reach it through
flutter pubin a Flutter project, ordart pubin a plain Dart project. Both run the same resolver. - pubspec.yaml is the file in your project that says which packages you want and which versions you accept.
The download goes to a shared cache on your machine, not into your project folder. Several projects can use the same cached copy of a package version. The default cache location is ~/.pub-cache on macOS and Linux and %LOCALAPPDATA%\Pub\Cache on Windows. You can change it with the PUB_CACHE environment variable.
What a package contains
A published package is a folder with a known layout. A typical one looks like this:
my_package/
pubspec.yaml
README.md
CHANGELOG.md
LICENSE
lib/
my_package.dart
src/
internal_helpers.dart
example/
test/Some of these files matter for how you consume the package:
pubspec.yamlholds the package name, version, and its own dependencies.lib/holds the public code. Anything directly underlib/is part of the public API.lib/src/holds implementation. Dart convention says you should not import fromsrc/in someone else's package. The package author may rename or remove those files in any release, and the version number will not warn you.CHANGELOG.mdandREADME.mdappear on the package's page on pub.dev. They are the first place to look when something breaks after an upgrade.
Each package version is immutable once published. The author can mark a version as retracted, which tells the resolver to avoid it, but the content cannot be changed. That immutability is what makes a lockfile reliable.
Packages and plugins
pub.dev hosts several kinds of package. Two are important in Flutter work, and people often mix them up.
| Kind | What it contains | Needs native setup | Example |
|---|---|---|---|
| Dart package | Only Dart code | No | `http`, `collection`, `intl` |
| Flutter package | Dart code that depends on the Flutter framework, often widgets | No | `google_fonts`, `flutter_svg` |
| Plugin | Dart API plus platform-specific code (Kotlin, Swift, C++, JavaScript) | Sometimes | `shared_preferences`, `url_launcher`, `camera` |
A Dart package is written purely in Dart and does not import Flutter. It runs anywhere Dart runs: a Flutter app, a command-line tool, or a server.
A Flutter package is also pure Dart, but it imports package:flutter/.... It therefore works only inside Flutter. Many widget libraries belong here.
A plugin is a package that talks to the operating system or to a browser API. Dart code cannot read the camera or open the system share sheet directly. The plugin includes native code for each platform it supports and a Dart API that hides the differences. At runtime, Dart calls into the native side through a platform channel or through FFI (foreign function interface, Dart's way of calling C-compatible code).
Most plugins published by the Flutter team follow the federated plugin layout. The plugin is split into several packages: an app-facing package that you depend on, a platform interface package that defines the contract, and one implementation package per platform. For example, depending on url_launcher also pulls in url_launcher_android, url_launcher_ios, and others. You list only the app-facing package. The pub tool resolves the rest, and you will see the extra packages in pubspec.lock.
A related kind is the FFI plugin, which bundles native C, C++, or Rust code and calls it through FFI instead of platform channels. You add it the same way, but it may need a native toolchain at build time.
Why the distinction matters
You treat all three kinds the same when you add them: one entry in pubspec.yaml. The differences show up later:
- Plugins can fail at build time or run time on a platform where no implementation exists. A plugin that supports only Android and iOS will throw
MissingPluginExceptionor fail to compile if you run the app on Linux. - Plugins may need permissions or configuration in
AndroidManifest.xml,Info.plist, orindex.html. Pure Dart packages never do. - Plugins make builds slower and larger, because the native code is compiled in.
- Pure Dart packages are easier to test, because they need no device or emulator.
How to tell what kind of package you are looking at
Every package page on pub.dev shows tags near the top, and these are the quickest check:
- Platform tags such as Android, iOS, Linux, macOS, web, and Windows list where the package is declared to work.
- SDK tags such as Dart and Flutter show whether it needs Flutter. A package tagged only Dart works without Flutter. A package tagged Flutter imports the framework.
- A plugin label appears for packages that declare platform implementations.
The package's own pubspec.yaml, viewable on the page, is the source of the truth. A plugin declares itself with a flutter: plugin: section:
name: example_plugin
description: A plugin that reads the battery level.
version: 1.0.0
environment:
sdk: ^3.3.0
flutter: '>=3.19.0'
flutter:
plugin:
platforms:
android:
package: com.example.example_plugin
pluginClass: ExamplePlugin
ios:
pluginClass: ExamplePluginA pure Dart package has no such section. You will rarely write this yourself, but reading it helps you understand what a package requires of your app.
What pub.dev shows about each package
pub.dev adds its own analysis to every package. You will use these signals in the chapter on choosing a package, but you should know where they live now:
- Likes: how many users bookmarked the package.
- Pub points: a score out of 160 from automated checks, such as documentation, platform support, static analysis, and up-to-date dependencies.
- Popularity: a percentile based on how many projects use it.
- Publisher: a verified publisher badge shows that the package belongs to a domain-verified organization, for example
dart.devorfirebase.google.com. - Tabs: Readme, Changelog, Example, Installing, Versions, Scores. The Installing tab shows the exact command to add the package.
A fast look at these tells you if a package is maintained. A high popularity number with an old last-publish date can still be a risk, and a new package with few likes can be excellent. No single number decides the question.
Edge cases and common mistakes
- Name collisions. Two packages can export the same class name, such as
Client. Use import prefixes (import 'package:http/http.dart' as http;) to avoid clashes. - Discontinued packages. pub.dev marks a package as discontinued when its author abandons it, sometimes with a suggested replacement. Do not add these to new code.
- Unlisted packages. An author can hide a package from search while keeping it installable. Having an exact name does not prove the package is current.
- Typosquatting. Look-alike names exist. Check the publisher and the repository link before you add an unfamiliar package.
- Packages are code that runs in your app. A dependency has the same access as your own code. Plugin native code can use any permission your app holds. Review what you add.
- Transitive dependencies. Each package can depend on other packages, and so on. Your app includes all of them, even though you wrote only one line in
pubspec.yaml.
Exercise
You add a package to a Flutter app and it runs on Android and iOS. When you launch the same app on Linux desktop, it throws MissingPluginException. Which kind of package is it most likely, and where on its pub.dev page do you confirm the cause?
Show answer
It is most likely a plugin, because plugins include per-platform native code and this one has no Linux implementation. Confirm on the package page by checking the platform tags near the top, which would list Android and iOS but not Linux, and by reading the flutter: plugin: platforms: section of its pubspec.
Exercise
A teammate says, "I only added url_launcher, so my app has one dependency." Why is that not accurate, and how would you see the full set?
Show answer
url_launcher is a federated plugin, so it depends on a platform interface package and implementation packages such as url_launcher_android and url_launcher_ios, which in turn may have their own dependencies. These transitive dependencies are resolved by the pub tool and recorded in pubspec.lock. You can see them there or by running flutter pub deps.