Sub topic 8
Dev dependencies for tooling and tests
This chapter assumes you know basic Dart syntax, can run Flutter commands in a terminal, and understand the main sections of pubspec.yaml. You will learn how to keep development-only packages separate from packages your app needs at runtime.
Runtime dependencies and dev dependencies
A runtime dependency is a package that the application needs when it runs. An HTTP client, database library, or state-management package usually belongs in dependencies.
A dev dependency is a package used while developing, testing, or building the application. Test frameworks, code generators, linters, and mock libraries usually belong in dev_dependencies.
Keeping these groups separate communicates the purpose of each package and prevents production code from depending on tools it does not use.
| Package use | YAML section | Examples |
|---|---|---|
| Required by the running app | `dependencies` | HTTP client, database, router |
| Required for development or testing | `dev_dependencies` | Test framework, mock library, code generator |
Flutter projects normally contain flutter_test as a dev dependency:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.4.0The flutter_test package provides Flutter's testing APIs. The build_runner package runs code-generation tasks. Neither package is needed to display the release application on a user's device.
Adding a dev dependency
Use flutter pub add with the dev: prefix:
flutter pub add dev:build_runnerThis command updates pubspec.yaml and resolves the package. You can also edit the file directly, then run:
flutter pub getAfter adding a package, inspect the change before committing it. Check that the package is in the correct section and that its version constraint fits the project.
A test package can be added in the same way:
flutter pub add dev:mocktailYou can then use the package from a test file:
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('shows the ready message', (tester) async {
await tester.pumpWidget(const MaterialApp(
home: Text('Ready'),
));
expect(find.text('Ready'), findsOneWidget);
});
}The test imports flutter_test, creates a small widget tree, and checks the rendered text. This code runs when you execute flutter test; it is not part of the application's runtime behavior.
Generated code and build tools
Some dev dependencies generate Dart files from annotations or schemas. A common workflow uses build_runner:
dart run build_runner buildThe generated files may be imported by runtime code, but the generator itself remains a dev dependency. The distinction is based on who needs the package:
- The application needs the generated output.
- Developers and CI need the generator to create or update that output.
If a package is imported by lib/ code that runs in the app, place that package in dependencies, even if a related generator belongs in dev_dependencies.
Testing the dependency setup
Run the following commands after changing development dependencies:
flutter pub get
flutter analyze
flutter testflutter pub get resolves the dependency graph. flutter analyze checks Dart code and configuration. flutter test verifies that the test-only packages and test files work together.
The resolved versions are recorded in pubspec.lock. Commit this file for an application so local development and continuous integration use the same resolved versions. When a dependency is used only by tests, its version can still affect the lockfile because Pub resolves the project's complete dependency graph.
Exercise
Where should a package go if code in lib/ imports it when the application runs?
Show answer
Put it in dependencies. Put only its separate test or code-generation tools in dev_dependencies.