dart_style 1.0.9

  • README.md
  • Installing
  • Versions
  • 96

The dart_style package defines an automatic, opinionated formatter for Dart code. It replaces the whitespace in your program with what it deems to be the best formatting for it. Resulting code should follow the Dart style guide but, moreso, should look nice to most human readers, most of the time.

The formatter handles indentation, inline whitespace and (by far the most difficult), intelligent line wrapping. It has no problems with nested collections, function expressions, long argument lists, or otherwise tricky code.

The formatter turns code like this:

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


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

The formatter will never break your code—you can safely invoke it automatically from build and presubmit scripts.

Getting dartfmt

Dartfmt is included in the Dart SDK, so you might want to add the SDK's bin directory to your system path.

If you want to make sure you are running the latest version of dartfmt, you can globally activate the package from the dart_style package on pub.dartlang.org, and let pub put its executable on your path:

$ pub global activate dart_style
$ dartfmt ...

If you don't want dartfmt on your path, you can run it explicitly:

$ pub global activate dart_style --no-executables
$ pub global run dart_style:format ...

Using dartfmt

IDEs and editors that support Dart usually provide easy ways to run the formatter. For example, in WebStorm you can right-click a .dart file and then choose Reformat with Dart Style.

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.

Dartfmt takes a list of paths, which can point to directories or files. If the path is a directory, it processes every .dart file in that directory or any of its subdirectories. If no file or directory is specified, dartfmt reads from standard input.

By default, it formats each file and just prints the resulting code to stdout. If you pass -w, it overwrites your existing files with the formatted results.

You may pass a -l option to control the width of the page that it wraps lines to fit within, but you're strongly encouraged to keep the default line length of 80 columns.

Validating files

If you want to use the formatter in something like a presubmit script or commit hook, you can use the -n dry run option. If you specify -n, the formatter prints the paths of the files whose contents would change if the formatter were run normally. If it prints no output, then everything is already correctly formatted.

Using the dart_style API

The package also exposes a single dart_style library containing a programmatic API for formatting code. Simple usage looks like this:

import 'package:dart_style/dart_style.dart';

main() {
  var formatter = new DartFormatter();

  try {
    library an_entire_compilation_unit;

    class SomeClass {}

  } on FormatterException catch (ex) {

Other resources

  • Before sending an email, see if you are asking a frequently asked question.

  • Before filing a bug, or if you want to understand how work on the formatter is managed, see how we track issues.


  • Updated tests. No user-facing changes.


  • Support v1 of pkg/args.


  • Format multiline strings as block arguments (#570).
  • Fix call to analyzer API.
  • Support assert in initializer list experimental syntax (#522).


  • Support URIs in part-of directives (#615).


  • Support the latest version of pkg/analyzer.


  • Ensure formatter throws an exception instead of introducing non-whitespace changes. This sanity check ensures the formatter does not erase user code when the formatter itself contains a bug.
  • Preserve type arguments in generic typedefs (#619).
  • Preserve type arguments in function expression invocations (#621).


  • Preserve type arguments in generic function-typed parameters (#613).


  • Support new generic function typedef syntax (#563).


  • Ensure space between - and -- (#170).
  • Preserve a blank line between enum cases (#606).


  • Handle mixed block and arrow bodied function arguments uniformly (#500).
  • Don't add a spurious space after "native" (#541).
  • Handle parenthesized and immediately invoked functions in argument lists like other function literals (#566).
  • Preserve a blank line between an annotation and the first directive (#571).
  • Fix splitting in generic methods with => bodies (#584).
  • Allow splitting between a parameter name and type (#585).
  • Don't split after < when a collection is in statement position (#589).
  • Force a split if the cascade target has non-obvious precedence (#590).
  • Split more often if a cascade target contains a split (#591).
  • Correctly report unchanged formatting when reading from stdin.


  • Don't discard type arguments on method calls with closure arguments (#582).


  • Support covariant modifier on methods.


  • Update to analyzer 0.29.3. This should make dart_style a little more resilient to breaking changes in analyzer that add support for new syntax that dart_style can't format yet.


  • Support generic method parameters as well as arguments.


  • Add support for assert() in constructor initializers.
  • Correctly indent the right-hand side of is and as expressions.
  • Avoid splitting in index operators when possible.
  • Support generic methods (#556).


  • Fix test to not depend on analyzer error message.


  • Widen dependency on analyzer to allow 0.29.x.


  • Handle metadata annotations before parameters with trailing commas (#520).
  • Always split enum declarations if they end in a trailing comma (#529).
  • Add --set-exit-if-changed to set the exit code on a change (#365).


  • Require analyzer 0.27.4, which makes trailing commas on by default.


  • Format parameter lists with trailing commas like argument lists (#447).


  • Make it strong mode clean.
  • Put labels on their own line (#43).
  • Gracefully handle IO errors when failing to overwrite a file (#473).
  • Add a blank line after local functions, to match top level ones (#488).
  • Improve indentation in non-block-bodied control flow statements (#494).
  • Better indentation on very long return types (#503).
  • When calling from JS, guess at which error to show when the code cannot be parsed (#504).
  • Force a conditional operator to split if the condition does (#506).
  • Preserve trailing commas in argument and parameter lists (#509).
  • Split arguments preceded by comments (#511).
  • Remove newlines after script tags (#513).
  • Split before a single named argument if the argument itself splits (#514).
  • Indent initializers in multiple variable declarations.
  • Avoid passing an invalid Windows file URI to analyzer.
  • Always split comma-separated sequences that contain a trailing comma.


  • Support deploying an npm package exporting a formatCode method.


  • Better handling for long collections with comments (#484).


  • Support messages in assert() (#411).
  • Don't put spaces around magic generic method annotation comments (#477).
  • Always put member metadata annotations on their own line (#483).
  • Indent functions in named argument lists with non-functions (#478).
  • Force the parameter list to split if a split occurs inside a function-typed parameter.
  • Don't force a split for before a single named argument if the argument itself splits.


  • Upgrade to analyzer 0.27.0.
  • Format configured imports and exports.


  • --version command line argument (#240).
  • Split the first . in a method chain if the target splits (#255).
  • Don't collapse states that differ by unbound rule constraints (#424).
  • Better handling for functions in method chains (#367, #398).
  • Better handling of large parameter metadata annotations (#387, #444).
  • Smarter splitting around collections in named parameters (#394).
  • Split calls if properties in a chain split (#399).
  • Don't allow splitting inside empty functions (#404).
  • Consider a rule live if it constrains a rule in the overflow line (#407).
  • Allow splitting in prefix expressions (#410).
  • Correctly constrain collections in argument lists (#420, #463, #465).
  • Better indentation of collection literals (#421, #469).
  • Only show a hidden directory once in the output (#428).
  • Allow splitting between type and variable name (#429, #439, #454).
  • Better indentation for binary operators in => bodies (#434.
  • Tweak splitting around assignment (#436, #437).
  • Indent multi-line collections in default values (#441).
  • Don't drop metadata on part directives (#443).
  • Handle if statements without curly bodies better (#448).
  • Handle loop statements without curly bodies better (#449).
  • Allow splitting before get and set (#462).
  • Add --indent to specify leading indent (#464).
  • Ensure collection elements line split separately (#474).
  • Allow redirecting constructors to wrap (#475).
  • Handle index expressions in the middle of call chains.
  • Optimize splitting lines with many rules.


  • Treat functions nested inside function calls like block arguments (#366).


  • Smarter indentation for function arguments (#369).


  • Optimize splitting complex lines (#391).


  • Allow splitting between adjacent strings (#201).
  • Force multi-line comments to the next line (#241).
  • Better splitting in metadata annotations in parameter lists (#247).
  • New optimized line splitter (#360, #380).
  • Allow splitting after argument name (#368).
  • Parsing a statement fails if there is unconsumed input (#372).
  • Don't force for fully split if initializers or updaters do (#375, #377).
  • Split before deferred (#381).
  • Allow splitting on as and is expressions (#384).
  • Support null-aware operators (?., ??, and ??=) (#385).
  • Allow splitting before default parameter values (#389).


  • BREAKING: The indent argument to new DartFormatter() is now a number of spaces, not indentation levels.

  • This version introduces a new n-way constraint system replacing the previous binary constraints. It's mostly an internal change, but allows us to fix a number of bugs that the old solver couldn't express solutions to.

    In particular, it forces argument and parameter lists to go one-per-line if they don't all fit in two lines. And it allows function and collection literals inside expressions to indent like expressions in some contexts. (#78, #97, #101, #123, #139, #141, #142, #143, et. al.)

  • Indent cascades more deeply when the receiver is a method call (#137).

  • Preserve newlines in collections containing line comments (#139).

  • Allow multiple variable declarations on one line if they fit (#155).

  • Prefer splitting at "." on non-identifier method targets (#161).

  • Enforce a blank line before and after classes (#168).

  • More precisely control newlines between declarations (#173).

  • Preserve mandatory newlines in inline block comments (#178).

  • Splitting inside type parameter and type argument lists (#184).

  • Nest blocks deeper inside a wrapped conditional operator (#186).

  • Split named arguments if the positional arguments split (#189).

  • Re-indent line doc comments even if they are flush left (#192).

  • Nest cascades like expressions (#200, #203, #205, #221, #236).

  • Prefer splitting after => over other options (#217).

  • Nested non-empty collections force surrounding ones to split (#223).

  • Allow splitting inside with and implements clauses (#228, #259).

  • Allow splitting after = in a constructor initializer (#242).

  • If a => function's parameters split, split after the => too (#250).

  • Allow splitting between successive index operators (#256).

  • Correctly indent wrapped constructor initializers (#257).

  • Set failure exit code for malformed input when reading from stdin (#359).

  • Do not nest blocks inside single-argument function and method calls.

  • Do nest blocks inside => functions.


  • Allow using analyzer 0.26.0-alpha.0.


  • Use the new test package runner internally.


  • Update to latest analyzer and args packages.
  • Allow cascades with repeated method names to be one line.


  • Update to latest analyzer (#177).
  • Don't discard annotations on initializing formals (#197).
  • Optimize formatting deeply nested expressions (#108).
  • Discard unused nesting level to improve performance (#108).
  • Discard unused spans to improve performance (#108).
  • Harden splits that contain too much nesting (#108).
  • Try to avoid splitting single-element lists (#211).
  • Avoid splitting when the first argument is a function expression (#211).


  • Allow passing in selection to preserve through command line (#194).

0.1.5+1, 0.1.5+2, 0.1.5+3

  • Fix test files to work in main Dart repo test runner.


  • Change executable name from dartformat to dartfmt.


  • Don't mangle comma after function-typed initializing formal (#156).
  • Add --dry-run option to show files that need formatting (#67).
  • Try to avoid splitting in before index argument (#158, #160).
  • Support await for statements (#154).
  • Don't delete commas between enum values with doc comments (#171).
  • Put a space between nested unary - calls (#170).
  • Allow -t flag to preserve compatibility with old formatter (#166).
  • Support --machine flag for machine-readable output (#164).
  • If no paths are provided, read source from stdin (#165).


  • Split different operators with the same precedence equally (#130).
  • No spaces for empty for loop clauses (#132).
  • Don't touch files whose contents did not change (#127).
  • Skip formatting files in hidden directories (#125).
  • Don't include trailing whitespace when preserving selection (#124).
  • Force constructor initialization lists to their own line if the parameter list is split across multiple lines (#151).
  • Allow splitting in index operator calls (#140).
  • Handle sync and async syntax (#151).
  • Indent the parameter list more if the body is a wrapped "=>" (#144).


  • Move split conditional operators to the beginning of the next line.


  • Support formatting enums (#120).
  • Handle Windows line endings in multiline strings (#126).
  • Increase nesting for conditional operators (#122).

1. Depend on it

Add this to your package's pubspec.yaml file:

  dart_style: "^1.0.9"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dart_style/dart_style.dart';
Version Uploaded Documentation Archive
1.0.9 Jan 4, 2018 Go to the documentation of dart_style 1.0.9 Download dart_style 1.0.9 archive
1.0.8 Sep 21, 2017 Go to the documentation of dart_style 1.0.8 Download dart_style 1.0.8 archive
1.0.7 Jul 6, 2017 Go to the documentation of dart_style 1.0.7 Download dart_style 1.0.7 archive
1.0.6 May 10, 2017 Go to the documentation of dart_style 1.0.6 Download dart_style 1.0.6 archive
1.0.5 May 4, 2017 Go to the documentation of dart_style 1.0.5 Download dart_style 1.0.5 archive
1.0.4 Apr 26, 2017 Go to the documentation of dart_style 1.0.4 Download dart_style 1.0.4 archive
1.0.3 Apr 6, 2017 Go to the documentation of dart_style 1.0.3 Download dart_style 1.0.3 archive
1.0.1 Mar 24, 2017 Go to the documentation of dart_style 1.0.1 Download dart_style 1.0.1 archive
1.0.0 Mar 21, 2017 Go to the documentation of dart_style 1.0.0 Download dart_style 1.0.0 archive
0.2.16 Feb 3, 2017 Go to the documentation of dart_style 0.2.16 Download dart_style 0.2.16 archive

All 45 versions...


This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Jan 17, 2018
  • Dart: 2.0.0-dev.15.0
  • pana: 0.9.1


Describes how popular the package is relative to other packages. [more]
93 / 100
Code health derived from static analysis. [more]
100 / 100
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]


Detected platforms: Flutter, server

Multiple platform identified in libraries.


  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use dart_style.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
analyzer ^0.30.0 0.30.0+4 0.31.0-alpha.2
args >=0.12.1 <2.0.0 1.2.0
path >=1.0.0 <2.0.0 1.5.1
source_span >=1.4.0 <2.0.0 1.4.0
Transitive dependencies
charcode 1.1.1
cli_util 0.1.2+1
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
csslib 0.14.1
front_end 0.1.0-alpha.4.1 0.1.0-alpha.7
glob 1.1.5
html 0.13.2+2
isolate 1.1.0
kernel 0.3.0-alpha.1.1 0.3.0-alpha.4
logging 0.11.3+1
meta 1.1.2
package_config 1.0.3
plugin 0.2.0+2
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+3
watcher 0.9.7+6
Dev dependencies
async >=1.0.0 <=2.0.0 2.0.3
browser >=0.10.0 <0.11.0
grinder ^0.8.0
js ^0.6.0
node_preamble ^1.0.0
pub_semver ^1.2.3
test >=0.12.0 <0.13.0
test_descriptor ^1.0.0
test_process ^1.0.0
yaml ^2.0.0 2.1.13