Sub topic 1
What pub.dev Is and How Packages and Plugins Differ
Prerequisites
This chapter assumes that you know basic Dart syntax, have created a Flutter project with flutter create, can run commands in a terminal, and can read basic YAML.
The pub.dev ecosystem
pub.dev is the public package repository for Dart and Flutter. Developers publish reusable code there so other projects can install it through the Dart package manager, called pub.
A package can provide almost anything that does not require direct access to a device. Examples include:
- JSON serialization
- Date and time utilities
- HTTP clients
- State-management tools
- Validation helpers
- User-interface widgets
A package is identified by a name and a version. For example, the http package provides Dart APIs for sending HTTP requests.
The package page usually contains documentation, example code, a changelog, license information, and supported versions. Read these details before adding a dependency. A package becomes part of your build, so its maintenance and security affect your application.
Package versus plugin
In Flutter, a package is reusable Dart or Flutter code. A plugin is a package that also connects Dart code to platform-specific code, such as Android, iOS, macOS, Windows, Linux, or web APIs.
For example, a package that formats currency can run entirely in Dart. A plugin that opens the device camera must communicate with the operating system. That communication may require platform configuration.
The distinction is practical rather than absolute. Every plugin is distributed as a package, but many packages are not plugins.
| Type | Main implementation | Typical use | Platform setup |
|---|---|---|---|
| Dart package | Dart code | Parsing, networking, formatting | Usually none |
| Flutter package | Dart and Flutter widgets | UI components and app utilities | Usually none |
| Plugin | Dart plus native or web code | Camera, location, storage | Often required |
When evaluating a plugin, check its platform list and setup instructions. A plugin may support Android and iOS but not web. Another may support web with different behavior. A package page's platform indicators are useful, but the documentation describes the actual limitations.
Evaluating a package before adding it
Do not choose a package only because its name matches your search. Consider several signals together:
- Publisher: Prefer a known organization or a verified publisher when the package handles sensitive data or platform access.
- Popularity: Likes and download activity can indicate adoption, but they do not prove quality.
- Score: pub.dev calculates a score from factors such as documentation, conventions, and static analysis. Treat it as evidence, not a guarantee.
- Maintenance: Check the latest release, commit history, open issues, and changelog.
- Platform support: Confirm that the package supports every platform your application targets.
- License: Make sure the license fits your project.
- Dependencies: A small package with few dependencies may be easier to audit and update.
A popular package can still be unsuitable. For example, it may lack a feature you need, require an unsupported platform, or introduce a large dependency tree.
Adding a package
From the root directory of a Flutter project, use the Flutter wrapper around pub:
flutter pub add httpThis command updates pubspec.yaml and resolves a compatible version. You can then inspect the package page and read its usage examples before importing it.
A plugin needs the same package-management step, but it may require extra native configuration afterward. Follow the plugin's installation instructions for permissions, minimum platform versions, manifest entries, or URL schemes.
Exercise
What is the key difference between a Flutter package and a plugin?
Show answer
A package provides reusable Dart or Flutter code, while a plugin also bridges Dart code to platform-specific or web APIs and may require platform setup.