Sub topic 4
Adding a Package with `flutter pub add`
This chapter assumes a Flutter project created with flutter create, basic Dart syntax, terminal commands, and basic YAML. You will add the http package, inspect the project changes, and use the package from Dart code.
Why use `flutter pub add`?
A package is reusable Dart or Flutter code published for other projects. The http package provides functions for making HTTP requests. Flutter uses Dart's package manager, called pub, to find compatible package versions and download their dependencies.
The command-line workflow is safer than manually typing a version because Flutter can choose the latest compatible version and update the dependency metadata for you.
Before adding a package, inspect its pub.dev page. Check:
- Whether the package is actively maintained.
- Its publisher and popularity.
- Its pub.dev score.
- Supported platforms, such as Android, iOS, web, Windows, macOS, or Linux.
- The package's documentation and license.
- Whether its API matches your application needs.
A high score does not guarantee that a package is the right choice. Platform support and maintenance history matter just as much.
Add the package
Open a terminal in your Flutter project's root directory. This directory contains pubspec.yaml. Run:
flutter pub add httpThe command adds the latest compatible version of http under dependencies in pubspec.yaml. It also resolves the dependency and downloads the package.
Your file will contain a section similar to this:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0The exact version may be newer. The caret (^) allows compatible releases from 1.2.0 up to, but not including, 2.0.0. Pub may choose a later 1.x release when one is available and compatible with the rest of your dependency graph.
You can edit pubspec.yaml directly when you need precise control, but keep indentation consistent. YAML uses indentation to express structure.
Import and call the API
After adding a package, import its library with a package: URI. The as http prefix gives the library a short name and prevents clashes with other names.
import 'package:http/http.dart' as http;
Future<String> fetchBody() async {
final response = await http.get(
Uri.parse('https://example.com'),
);
if (response.statusCode == 200) {
return response.body;
}
throw Exception('Request failed: ${response.statusCode}');
}This function is asynchronous because network requests take time. await pauses the function until the response arrives without blocking the application's main isolate. The status-code check prevents the app from treating an error response as successful data.
What changes in the project?
Adding a package normally affects two files:
pubspec.yamlrecords the dependency constraint that your project requests.pubspec.lockrecords the exact versions selected by dependency resolution.
The lockfile makes local development and application builds more repeatable. Do not manually edit it to solve a version problem. Change the constraint or package choice, then run pub commands again.
Common errors
If pub reports that it cannot find a package, check the package name and your network connection. If version solving fails, another dependency may require an incompatible version. Read the conflict lines before changing constraints. The fix might be upgrading the other package, selecting a different package version, or replacing the package.
A package that works in Dart code may still need platform setup if it is a Flutter plugin. For example, a plugin may require Android permissions, iOS configuration, or web-specific settings. Read its installation instructions before testing on a device.
Exercise
Which command adds the http package to the current Flutter project, and where is the package recorded?
Show answer
Run flutter pub add http from the project root. Pub records the requested constraint in pubspec.yaml and the resolved exact version in pubspec.lock.