Sub topic 8
Dev dependencies for tooling and tests
This chapter covers the dev_dependencies section of pubspec.yaml. It assumes you can add a package with flutter pub add, read a caret constraint such as ^1.2.0, and know that pubspec.lock records resolved versions. Earlier chapters covered all three.
What a dev dependency is
A dev dependency is a package your project needs while you develop it, but that your running app does not need. Typical examples are test frameworks, linters, and code generators.
Pub keeps these in a separate section of pubspec.yaml:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
build_runner: ^2.4.0Both sections use the same syntax for versions, path, and git entries. The difference is in who sees the package. Your own code can import either kind. Other projects that depend on yours only receive your dependencies.
Why the split matters
- Resolution scope. If you publish a package, its consumers never download or resolve your dev dependencies. A heavy tool such as
build_runnertherefore adds no constraints to their projects. - Intent. A reader of
pubspec.yamlcan tell which packages the shipped code relies on and which are only tooling. - Release builds. Flutter compiles only the code reachable from
lib/throughmain(). A package that is only imported fromtest/is never part of the app. The pubspec section does not control this. Import reachability does.
The last point has a consequence. Putting a package in dev_dependencies does not remove it from your app if code under lib/ imports it. Dart's analyzer warns about this with the depend_on_referenced_packages lint, and a package that lib/ needs but that is only in dev_dependencies will fail for anyone who consumes your package.
Adding a dev dependency
Use the dev: prefix with flutter pub add:
flutter pub add dev:build_runner
flutter pub add dev:mockitoThis writes the entry under dev_dependencies with a caret constraint on the latest compatible version, then runs pub get. Without the prefix, the package lands in dependencies.
Moving a package between sections is a manual edit. Cut the line from one section, paste it into the other, and run flutter pub get. Alternatively, run flutter pub remove and then add it again with the right prefix.
The packages you already have
A project created with flutter create starts with two dev dependencies.
| Package | Purpose |
|---|---|
| `flutter_test` | Test runner support, `testWidgets`, `WidgetTester`, and matchers. It comes from the Flutter SDK, so it uses `sdk: flutter` instead of a version. |
| `flutter_lints` | A recommended set of lint rules, enabled through `analysis_options.yaml`. |
flutter_test has no version number because the SDK pins it. Upgrading Flutter changes it. You cannot upgrade it with flutter pub upgrade.
Common dev dependency categories
Testing
flutter_test covers unit and widget tests. Mocking packages such as mockito or mocktail replace real collaborators in tests. For end-to-end tests on a device, integration_test is also an SDK package:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
mocktail: ^1.0.0Linting
flutter_lints supplies rules, and you opt in through analysis_options.yaml:
include: package:flutter_lints/flutter.yaml
linter:
rules:
avoid_print: trueRun flutter analyze to see the results. Because the lint package is a dev dependency, it affects analysis only.
Code generation
Packages such as build_runner, json_serializable, and freezed generate Dart files. The pattern is usually split across both sections. The annotation package goes in dependencies because generated code refers to it at runtime. The generator goes in dev_dependencies.
dependencies:
json_annotation: ^4.9.0
dev_dependencies:
build_runner: ^2.4.0
json_serializable: ^6.8.0Then run the generator:
dart run build_runner build --delete-conflicting-outputsAlways read the install instructions of a generator package. They tell you which half belongs in which section.
A worked example: a test that uses a dev dependency
This example adds mocktail and tests a class that fetches a greeting through an interface. Create lib/greeter.dart:
abstract class GreetingSource {
Future<String> fetchName();
}
class Greeter {
Greeter(this.source);
final GreetingSource source;
Future<String> greet() async {
final name = await source.fetchName();
return 'Hello, $name!';
}
}Add the package:
flutter pub add dev:mocktailThen create test/greeter_test.dart. Replace my_app with your project's name from pubspec.yaml:
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:my_app/greeter.dart';
class MockSource extends Mock implements GreetingSource {}
void main() {
test('greet uses the name from the source', () async {
final source = MockSource();
when(() => source.fetchName()).thenAnswer((_) async => 'Ada');
final greeter = Greeter(source);
expect(await greeter.greet(), 'Hello, Ada!');
verify(() => source.fetchName()).called(1);
});
}Run it:
flutter testThe test passes, and mocktail never appears in the compiled app because nothing under lib/ imports it.
Gotchas and common errors
Importing a dev dependency from `lib/`
If lib/ imports a package that is only in dev_dependencies, the app still builds and runs in your own project, because the package is installed locally. The mistake surfaces later, when you publish the package or when the analyzer reports depend_on_referenced_packages. Treat the lint as a real error.
Forgetting the package is missing in other contexts
A Docker build or CI step that installs only production dependencies can skip dev packages. If your build runs code generation, make sure that step runs flutter pub get with dev dependencies included, which is the default.
Version conflicts still apply
Dev dependencies take part in version solving for your own project. A build_runner release that needs a newer analyzer can conflict with another dev tool. The solver reports this like any other conflict. A later chapter shows how to read those messages.
Generated files and the lockfile
Commit pubspec.lock for apps. It pins dev dependencies too, so every teammate and CI run uses the same versions of your test and generator tools. Whether to commit generated .g.dart files is a team decision. Either commit them, or generate them in CI.
Dev dependencies are not a size optimization
Moving a package to dev_dependencies will not shrink your app. App size depends on what the compiler can reach from main(). Use the section to express intent and to protect consumers of a published package.
Quick reference
| Task | Command or location |
|---|---|
| Add a dev dependency | `flutter pub add dev:<package>` |
| Edit by hand | `dev_dependencies:` in `pubspec.yaml`, then `flutter pub get` |
| Run tests | `flutter test` |
| Run the linter | `flutter analyze` |
| Run a generator | `dart run build_runner build` |
Exercise
You are using json_serializable with json_annotation. Your lib/ code has import 'package:json_annotation/json_annotation.dart'; in a model file. Which package goes in dependencies, which goes in dev_dependencies, and why? What command adds each one?
Show answer
json_annotation goes in dependencies because the model file under lib/ imports it, so the running app needs it. Add it with flutter pub add json_annotation. json_serializable is only a code generator that runs at build time, so it goes in dev_dependencies. Add it with flutter pub add dev:json_serializable. build_runner, which runs the generator, is also a dev dependency, added with flutter pub add dev:build_runner.
Exercise
A teammate moves mocktail from dev_dependencies to dependencies to "make sure tests always work". Does this change what ends up in the release app if only files under test/ import it? What is the downside if this project is a published package?
Show answer
It does not change the release app, because Flutter compiles only code reachable from main(), and nothing under lib/ imports mocktail. The downside for a published package is that every consumer of the package must now resolve and download mocktail, which can add version constraints to their projects for a tool they never use.