Sub topic 6
Auditing Packages and Their Cost
Assume you know how to run Flutter commands, read pubspec.yaml, and distinguish direct dependencies from transitive dependencies. This chapter focuses on measuring the cost of packages before deciding whether to keep, replace, or remove them.
A package is not automatically expensive because it has many Dart files. Release compilation removes unreachable Dart code through tree shaking. However, packages can still add native libraries, assets, fonts, generated code, or initialization work that remains in the application.
Direct and transitive dependencies
A direct dependency is a package listed in your project's pubspec.yaml. A transitive dependency is a package required by one of those direct dependencies.
For example, your application might depend directly on an analytics package. That package may bring platform adapters, HTTP clients, JSON helpers, and native libraries with it.
flowchart TD
A[Application] --> B[analytics package]
A --> C[image package]
B --> D[platform adapter]
B --> E[HTTP package]
C --> F[native image library]A package's cost can include several separate parts:
| Cost | Typical source | How to investigate |
|---|---|---|
| Dart code | Classes and functions compiled into the app | Size analysis |
| Native code | Android, iOS, or other platform libraries | Size analysis and package documentation |
| Assets | Icons, models, configuration, or bundled files | Package source and APK contents |
| Fonts | Font files registered by the package | Asset inspection |
| Startup work | Plugin registration or initialization | Startup tracing and package code |
Use flutter pub deps to inspect the dependency graph:
flutter pub deps --style=compactLook for packages that appear under several branches. Removing one direct dependency may remove a large transitive subtree. Updating a package can also change that subtree, so review dependency changes rather than accepting them blindly.
Measure before replacing
Do not estimate a package's cost from its package size on the website. Published source size does not equal compiled application size. A small package may include a large native binary, while a larger Dart-only package may contribute very little after tree shaking.
First create a release size report:
flutter build apk --release \
--analyze-size \
--target-platform android-arm64The command writes a code-size analysis JSON file. Open that file with the Flutter DevTools app size tool. Compare a baseline build with a build where you temporarily remove or replace one package.
Change one dependency at a time. Record the resulting size difference and test the affected feature. This gives you evidence instead of confusing several simultaneous changes.
Decide whether a package is worth its cost
Ask these questions during an audit:
- Is the package used by a feature that users need?
- Does it provide native functionality that would be difficult to implement safely?
- Is there a smaller package with the same required capability?
- Does it add platform libraries, assets, or fonts that your application does not use?
- Does it perform work during plugin registration or application startup?
- Is it actively maintained and compatible with your supported platforms?
Prefer removing unused direct dependencies first. Then inspect large transitive branches. Avoid replacing a package only because another package has fewer lines of Dart code. Compare release size, startup behavior, reliability, platform support, maintenance, and migration effort.
A dependency audit should also check whether an import is needed only on a rarely used screen. In that case, deferred loading may reduce the initial code path, although it does not eliminate every platform-native cost.
A repeatable audit
Keep a small record for each candidate package:
- Build the baseline release artifact.
- Run the size analysis and save the result.
- Record the package's direct and transitive dependencies.
- Remove or replace only that package.
- Build again with the same target platform.
- Compare size, startup time, and feature tests.
- Keep the change only when its benefit justifies its risk.
Exercise
Why should you measure a package in a release build instead of judging its source size?
Show answer
Release builds apply compilation and tree shaking, while source size does not show the package's native libraries, retained Dart code, assets, or startup behavior.