build_runner

The build_runner package provides general-purpose commands for generating files,and for optionally testing the generated filesor serving both source and generated files.Read this page for an overview of using build_runner, with links towhere you can find more information.For details of using build_runner with a specific package,see the documentation for that package.

If you’re a web developer: You can use the webdev tool instead of directly using build_runner to build and serve web apps.

The buildrunner commands work with _builders—packagesthat use the Dart build systemto generate output files from input files.For example, the json_serializable and built_value_generatorpackages define builders that generate Dart code.

Although the Dart build system is a good alternative toreflection (which has performance issues) andmacros (which Dart’s compilers don’t support),it can do more than just read and write Dart code.For example, the sass_builder package implements a builder thatgenerates .css files from .scss and .sass files.

Setting up build_runner

To use build_runner, add a dev dependency on build_runnerto your app’s pubspec:

  1. dev_dependencies:
  2. # ···
  3. build_runner: ^1.0.0
  4. build_test: ^0.10.3

Depending on build_test is optional; do it if you’ll be testing your code.

As usual after pubspec.yaml changes, run pub get or pub upgrade:

  1. $ pub get

Using built-in commands

How you use the build_runner commands depends on whether you’re usingthe Dart SDK or the Flutter SDK.Here are examples of using the build_runner build command:

  1. $ # From a directory that contains a pubspec.yaml file:
  2. $ pub run build_runner build # Dart SDK
  3. $ flutter pub run build_runner build # Flutter SDK

The build_runner package includes the following commands:

  • build
  • Performs a one-time build.
  • serve
  • Runs a development server.Instead of directly using this command,you can use webdev serve,which has convenient default behavior.
  • test
  • Runs tests.
  • watch
  • Launches a build server that watches for edits to input files.Responds to changes by performing incremental rebuilds.

More information

If you’re working on web-specific code,see the webdev page.

For details on using build_runner, see the following: