dart format (original) (raw)

To update your code to follow theDart formatting guidelines, use the dart format command. This formatting follows what you get when using an IDE or editor with Dart support.

Specify files to format

#

To reformat one or more Dart files, provide a list of paths to the desired files or directories.

Provide the path to one file or directory. If you pass a directory path,dart format recurses into its subdirectories as well.

Example: To format all the Dart files in or under the current directory:

Specify multiple paths

#

To specify multiple files or directories, use a space-delimited list.

Example: To format all Dart files under the lib directory, plus one Dart file under the bin directory:

dart format lib bin/updater.dart

Prevent overwriting Dart files

#

By default, dart format overwrites the Dart files.

dart format -o show bin/my_app.dart

Notify when changes occur

#

To make dart format return an exit code when formatting changes occur, add the --set-exit-if-changed flag.

Use exit codes with continuous integration (CI) systems so they can trigger another action in response to the exit code.

dart format -o none --set-exit-if-changed bin/my_app.dart

dart format makes the following formatting changes:

To learn more about best practices for writing and styling Dart code, check out the Dart style guide.

Configuring formatter page width

#

When you run dart format, the formatter defaults to 80 character line length or shorter. If you'd like to configure the line length for your project, you can add a top-level formatter section to theanalysis_options.yaml file, like so:

analysis_options.yaml

yaml

formatter:
  page_width: 123

With the analysis options file typically at the root, the configured line length will apply to everything in the package.

You can also configure individual files' line length, overriding the analysis options file, with a marker comment at the top of the file before any other code:

dart

// dart format width=123

To learn about additional command-line options, use the dart help command or see the documentation for thedart_style package

Check out the formatter FAQ for more context behind formatting decisions.

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Page last updated on 2025-1-31. View source or report an issue.