上QQ阅读APP看书,第一时间看更新
Using the command-line tools
Some things can be done more easily on the command-line, or are simply not (yet) included in Dart Editor. These tools are found in dart-sdk/bin
. They consist of the following:
dart
: The standalone Dart VM to run Dart command-line apps, such as server-side scripts and server appsdartanalyzer
: This is used to check code staticallypub
: This is the package and repository managerdartfmt
: This is the code formatting tooldocgen
: This is the documentation generator tool
How to do it...
- For every tool, it might be useful to know or check its version. This is done with the
-- version
option such asdart --version
with a typical output of Dart VM version: 1.3.0 (Tue Apr 08 09:06:23 2014) on "windows_ia32". - The
dart –v –h
option lists and discusses all the possible options of the VM. Many tools also take the--package_root=<path>
or–p=<path>
option to indicate where the packages used in the imports reside on the filesystem. - dartanalyzer is written in Java and works in Dart Editor whenever a project is imported or Dart code is changed; it is started
dartanalyzer prorabbits.dart
with output:Analyzing prorabbits.dart...
No issues found (or possibly errors and hints to improve the code)
- The previous output verifies that the code conforms to the language specification https://www.dartlang.org/docs/spec/, pub functionality is built into Dart Editor, but the tool can also be used from the command line (refer to
test_pub
). To fetch packages (for example, for thetest_pub
app), use the following command in the folder wherepubspec.yaml
lives,pub get
, with a typical output as follows:Resolving dependencies... (6.6s) Got dependencies!
- A packages folder is created with symlinks to the central
package
cache on your machine. The latest versions are downloaded and the package versions are registered in thepubspec.lock
file, so that your app can only use these versions. - If you want to get a newer version of a package, use the
pub upgrade
command. You can use the–v
and-- trace
options to produce a detailed output to verify its workings.Tip
Always do a pub upgrade if the project you start working on already contains versions of packages!
- The
dartfmt
tool is also a built in Dart Editor. Right-click on any Dart file and choose Format from the context menu. This applies transformations to the code so that it conforms to the Dart Style Guide, which can be seen at then the default operation mode is cleaning up whitespace. Use the–t
option to apply code transforms such asdartfmt -t –w bank_terminal.dart
.
See also
- Solving problems when
pub get
fails - Compiling your app to JavaScript (for
pub build
) - Documenting your code from Chapter 2, Structuring, testing, and deploying an application
- Publishing your app to a pub (for pub publishing)
- Using snapshotting to start an app in Dart VM
- For additional information, refer to https://www.dartlang.org/tools/