Since Dart 3.7, how does dart format choose between the short and tall styles, and how do you configure page width and trailing commas?
answer
- chosen per file
- language version 3.7 is the switch
- formatter: page_width in analysis_options.yaml
- trailing_commas: preserve needs 3.8
- // dart format width= and off/on
basics
~20 sdart format picks the style per file from its language version: 3.6 or lower gets the old short style, 3.7 or later the tall style. Page width and trailing-comma behaviour are set under formatter: in analysis_options.yaml.
solid answer
~50 sThe formatter reads each file's **language version**, normally derived from the package's SDK constraint in `pubspec.yaml`. At 3.6 or lower it applies the old short style; at 3.7 or later it applies the **tall style**, which adds trailing commas to lists that split and removes them from lists that fit. So raising the lower SDK bound to 3.7 opts the package in. Configuration lives in `analysis_options.yaml`: `formatter: page_width: 100` changes the default of 80 for every file under that directory, and `formatter: trailing_commas: preserve` (language 3.8+) restores the old habit where a hand-written trailing comma forces a split. A `// dart format width=N` comment at the top of one file overrides the width, and `// dart format off` / `// dart format on` exempt a region. The CLI flag is now `--page-width`; `--line-length` is its legacy name.
code
yaml · 5 linesinclude: package:lints/recommended.yaml
formatter:
page_width: 100
trailing_commas: preservego deeper
Recall that the style follows the language version and that analysis_options.yaml holds the formatter section.
Explain the 3.7 switch, page_width and trailing_commas: preserve, and the width and off/on marker comments.
Plan an SDK-constraint bump on a large codebase so the one-time reformat lands as its own reviewed change.
Decide whether a team keeps the default width and tall-style comma handling or pays the cost of custom settings across packages.
## Two styles, chosen per file Dart 3.7 shipped a largely rewritten formatter with a new **tall style**, better suited to the declarative, deeply nested code common in Flutter widget trees. Rather than flip every project at once, the formatter decides **per file** using the file's **language version**: | Language version | Style | Trailing commas | |---|---|---| | 3.6 or lower | short (old) style | a trailing comma you write forces a split and is kept | | 3.7 or later | tall style | the formatter adds them to split lists and removes them from lists that fit | The language version usually comes from the **lower bound of the SDK constraint** in `pubspec.yaml`. Raising `environment: sdk:` to `^3.7.0` or higher therefore opts the whole package into the tall style the next time it is formatted. The formatter reads that version through the package configuration file, which is why `dart pub get` must have run. The release notes say the old style will not be supported forever; once most of the ecosystem is on 3.7 or later, support for it is planned to be removed. ## Project-wide configuration The formatter looks for an `analysis_options.yaml` in the file's directory and walks upward until it finds one. It reads a top-level `formatter` section: - **`page_width`** - the target line length. The default is 80. Setting it in one file applies to everything beneath it, and because analysis options support `include`, a shared options file can carry it across many packages. - **`trailing_commas: preserve`** - added in Dart 3.8 and off by default. When set, a trailing comma you add to an argument list, parameter list or similar construct **forces** it to split across lines even if it would fit, giving manual control over line breaks. Without it, the tall style removes a trailing comma from anything that fits on one line. These options require a language version of at least 3.7; `trailing_commas` requires 3.8. ## Per-file and per-region overrides 1. **`// dart format width=123`** placed before any code in a file overrides the page width for that file. It exists mainly for code generators, which format their output without knowing the surrounding options file. 2. **`// dart format off`** and **`// dart format on`** exempt the lines between them from formatting - useful for hand-aligned tables of numbers. The comments must match exactly, and regions cannot overlap or nest. ## The command-line flag `--page-width` sets the width on the command line. It replaced `--line-length` in 3.7; the old name still works for backwards compatibility. Project configuration is preferred because every developer, editor and CI job then reads the same value. ## Scenario: formatting a new command-line package A package created today with `dart create` gets an SDK constraint of `^` the current SDK version, so 3.7 or later, so it is formatted in the tall style from the first commit. If the team wants a width of 100, it adds `formatter: page_width: 100` to the generated `analysis_options.yaml` - not a flag in a script - so format-on-save and the CI check agree. ## Common mistakes - Expecting a hand-written trailing comma to force a split in tall style without `trailing_commas: preserve`. - Setting the width only in an editor setting, so CI formats differently. - Raising the SDK lower bound in a large repository without planning for the one-time reformat of every file. - Assuming the style is chosen per package; it is chosen per file, by that file's language version.
- What does // dart format width=30 at the top of a file do, and who is it mainly for?It overrides the page width for that one file, taking precedence over any `analysis_options.yaml`. It was added mainly for code generators, which format generated code immediately and cannot know the width a surrounding options file sets.
- Why might bumping a package's SDK lower bound from 3.6 to 3.7 produce a huge diff?The formatter picks the style from the language version, which follows that lower bound. Moving to 3.7 switches every file to the tall style, so the next format run rewrites trailing commas and line breaks across the package.
saying these in an interview costs you the question
- Thinks the formatter style is set by a command-line flag
- Believes a trailing comma always forces a split in tall style
- Sets page width only in the editor settings
- Assumes the style is chosen once per package rather than per file
- Thinks --line-length is the current flag name