Sub topic 11
Removing a package cleanly
This chapter assumes you know basic Dart syntax, have a Flutter project, can run terminal commands, and understand basic YAML. You will remove a package without leaving unused imports, configuration, or misleading dependency entries.
What removal changes
A package can appear in three places:
pubspec.yaml, which declares the direct dependency your project requests.pubspec.lock, which records the exact versions selected by dependency resolution.- Dart and platform code, which may import or configure the package.
Removing the dependency declaration is necessary, but it is not always sufficient. Your source code must stop using the package, and any platform setup must be reviewed.
A direct dependency is a package listed by your project. A transitive dependency is a package required by another package. Removing one direct dependency does not necessarily remove all of its transitive packages. Pub keeps any package still needed elsewhere.
Use `flutter pub remove`
The safest normal workflow is to let Flutter update pubspec.yaml for you:
flutter pub remove httpThe command removes http from the appropriate dependency section and resolves the dependency graph again. It also updates pubspec.lock when the project uses a lockfile.
Afterward, inspect the project:
git diff -- pubspec.yaml pubspec.lock
flutter analyze
flutter testflutter analyze checks the code for static problems, such as imports that no longer resolve. Tests check behavior that analysis cannot prove.
You can also remove the entry manually from pubspec.yaml, then run:
flutter pub getThe command is useful when you need to make several related YAML edits together. Keep the edit small and inspect the result before committing it.
Clean up imports and API calls
Search for imports from the removed package and delete or replace them. Then remove code that calls its APIs.
For example, after removing http, this import must disappear:
import 'package:http/http.dart' as http;Code that calls http.get, constructs package-specific types, or catches package-specific exceptions also needs attention. Replace that behavior with another implementation, or remove the feature if it is no longer needed.
Use your editor's project-wide search, or run a search command such as:
rg "package:http|http\." lib testThe search pattern should match the package you removed. Also search configuration files for the package name. Some plugins add entries to Android, iOS, web, or desktop files.
Check platform-specific cleanup
A Flutter package may be a plugin rather than a Dart-only library. A plugin includes native code or platform integration. Removing it can require additional cleanup.
| Package type | Typical cleanup |
|---|---|
| Dart-only library | Remove imports and direct API calls |
| Flutter plugin | Review Android, iOS, web, and desktop configuration |
| Development tool | Remove scripts, generated files, or configuration references |
For example, a plugin may have added permissions, initialization code, URL schemes, or build settings. Check the package documentation and compare platform files in your version-control diff.
Verify the dependency graph
After removal, confirm that the package is no longer a direct dependency:
flutter pub depsThis prints the complete dependency graph. The package might still appear as a transitive dependency. That is expected if another package requires it.
The following workflow shows the usual order:
Commit a focused change
Review all changed files:
git diff
git statusA clean removal normally includes the dependency declaration, lockfile changes, source cleanup, and any necessary platform cleanup. Avoid committing unrelated formatting or generated changes unless the package removal caused them.
Exercise
After running flutter pub remove some_package, the package still appears in flutter pub deps. Is removal broken?
Show answer
Not necessarily. It may be a transitive dependency required by another direct dependency. Check which package depends on it before taking further action.