dartfmt: The Dart Code Formatter

Use dartfmt to format your Dart code according to the conventions in the Dart Style Guide.

To format your code in Dart Editor, right-click in the editor pane to bring up the menu, and select the Format option.

The formatter fixes indentation and white space, and inserts line breaks where appropriate. Only white space is affected. Dartfmt is safe to run automatically. We encourage you to add it to your build scripts.

The following example shows a complex expression, with most white space removed, and the code that results when using dartfmt.

// BEFORE formatting
if (tag=='style'||tag=='script'&&(type==null||type == TYPE_JS
  tag=='link'&&(rel=='stylesheet'||rel=='import')) {}

Dartfmt converts this to:

// AFTER formatting
if (tag == 'style' ||
    tag == 'script' &&
        (type == null || type == TYPE_JS || type == TYPE_DART) ||
    tag == 'link' && (rel == 'stylesheet' || rel == 'import')) {}

Basic usage

Here’s a simple example of using dartfmt on the command line:

dartfmt test.dart

This command formats the test.dart file and writes the result to standard output. If you specify a directory instead of a file, dartfmt recurses through the directory and formats all files with a .dart extension.

If no file or directory is specified, dartfmt reads from standard input.


Common command-line options for dartfmt include:

-h or --help
Display help.
-l <value> or --max_line_length=<value>
Wrap any lines longer than the specified value. The default value is 80. Comments are not affected.
-n or --dry-run
Show which files would be modified, but make no changes.
-w or --overwrite
Write the output back into the source file. By default, the output goes to standard output.

More information