Sub topic 3
Dev dependencies for tooling and tests
This chapter assumes you know basic Dart syntax, have a project made with flutter create, can run commands in a terminal, and can read YAML. Dependencies are listed in pubspec.yaml.
Dev dependencies for tooling and tests
A dev dependency is a package that only your development work needs, such as a test helper or a code generator. The app does not ship it. Add one with the dev: prefix:
flutter pub add dev:build_runnerThis entry goes under dev_dependencies: in pubspec.yaml, separate from dependencies:.
Plugins that need platform setup
A plugin is a package that calls native code on Android, iOS, macOS, Windows, Linux, or the web. Adding one is sometimes not enough. Read the plugin's README for required steps, such as a permission in android/app/src/main/AndroidManifest.xml or a usage description in ios/Runner/Info.plist. Then run the app on a device, because native errors often appear only there.
Finding outdated packages and upgrading
flutter pub outdated
flutter pub upgradeflutter pub outdated lists packages with newer releases. flutter pub upgrade moves each package to the newest version that still fits its constraint, then rewrites pubspec.lock. To cross a major version, run flutter pub upgrade --major-versions.
Removing a package
flutter pub remove httpThis deletes the entry from pubspec.yaml and updates the lock file. Also delete any import lines that use the package, or the analyzer will report errors.
Understanding version solving failures
When pub cannot find versions that satisfy every constraint, it reports a message like this:
Because my_app depends on foo ^2.0.0 and bar ^1.0.0 depends on foo ^1.0.0, version solving failed.Fix the conflict in this order: check whether a newer release of the conflicting package accepts the version you need, loosen your own constraint, or remove one of the packages.
Using git and path dependencies
Use a path dependency for a package in a local folder, and a git dependency for one in a repository:
dependencies:
my_local_package:
path: ../my_local_package
my_forked_package:
git:
url: https://github.com/example/my_forked_package.git
ref: mainRun flutter pub get after editing. Pub records the exact git commit in pubspec.lock, so new commits on the branch do not change your build until you run flutter pub upgrade.
Dependency overrides and when to avoid them
A dependency_overrides section forces pub to use a version of a package, ignoring what other packages require:
dependency_overrides:
foo: ^2.0.0Exercise
Which section of pubspec.yaml should hold build_runner, and which command removes the http package?
Show answer
The devdependencies section holds buildrunner, added with flutter pub add dev:build_runner. Run flutter pub remove http to remove http.