正如 高效 Dart 语言指南 所说,在面对代码格式化的问题时,哪个参数更好是一种主观感受,无法完全解决。
Fortunately, you can use the dart format
tool —
from the command line or in your favorite Dart-savvy IDE —
to perform most of the drudge work of formatting your code.
For example, here’s how to format all the Dart files
under the current directory’s bin
, lib
, and test
directories:
幸运的是,您可以在命令行或可以使用 Dart 的 IDE 中使用
dart format
工具 来进行繁杂的大部分格式化工作。举个例子,下面是对当前目录下的 bin
、lib
和 test
文件夹下的所有 Dart 文件进行格式化的方法:
As Effective Dart says, when it comes to things like formatting, arguments about which is better are subjective and impossible to resolve. What we do know is that being consistent is objectively helpful. If two pieces of code look different it should be because they are different in some meaningful way. When a bit of code stands out and catches your eye, it should do so for a useful reason.
$ dart format -w bin lib test
然而,dart format 并不是面面俱到。为了避免造成一些破坏性改动,
dart format
仅仅会对空格(包含换行和缩进)进行操作。若需获得更多的指导,请查看高效 Dart 语言指南的 风格指南,特别是其中的 格式化指南 部分。
More information:
更多信息:
However, dart format can’t do it all.
To avoid making changes that might be unsafe, dart format
affects only whitespace.
For additional guidance, see the Effective Dart
style guidelines, especially the formatting guidelines.