Sub topic 1
What pub.dev Is and How Packages and Plugins Differ
This chapter explains where Flutter apps get reusable code, and how to tell a package from a plugin before you add either one.
Prerequisites
This chapter assumes you know basic Dart syntax, have a project created with flutter create, can run commands in a terminal, and can read simple YAML.
pub.dev and the Dart Package Ecosystem
pub.dev is the official repository for Dart and Flutter packages. A package is a folder of reusable code with a pubspec.yaml file that describes it. The pub tool downloads packages from pub.dev and makes them available to your project.
Packages and Plugins
A plugin is a special kind of package. It contains Dart code plus native code that talks to the operating system, such as Android, iOS, web, or desktop APIs. A pure Dart package has no native code, so it runs anywhere Dart runs.
| Kind | Contains | Typical use |
|---|---|---|
| Pure Dart package | Dart code only | Date formatting, JSON helpers |
| Flutter package | Dart widgets, no native code | Custom buttons, animations |
| Plugin | Dart API plus platform code | Camera, GPS, secure storage |
Where Dependencies Live: pubspec.yaml
Every project lists its packages in pubspec.yaml. Packages the app ships go under dependencies. Tools and test libraries go under dev_dependencies.
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutterThe ^1.2.0 is a caret constraint. It accepts version 1.2.0 and any later version below 2.0.0.
Choosing a Package: Score, Likes, Publisher, Platforms
Each pub.dev package page shows several signals:
- Pub points: a quality score, up to 160, based on documentation, conventions, and platform support.
- Likes: the number of users who liked the package.
- Popularity: an estimate of how often the package is used.
- Publisher: a verified domain shows the package comes from an identified organization.
- Platforms: the list of Android, iOS, web, and desktop targets the package supports.
> Check the platform list before you add a plugin. A plugin that lacks your target platform fails when you build for that platform.
Adding a Package with flutter pub add
Run this command in the project root:
flutter pub add httpThe command adds the latest compatible version to pubspec.yaml and runs pub get. Then import the package in Dart code:
import 'package:http/http.dart' as http;Exercise
Is a camera package a pure Dart package or a plugin, and why?
Show answer
It is a plugin, because accessing the camera requires native code on each platform.