Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 apps

  • dartanalyzer: This is used to check code statically

  • pub: This is the package and repository manager

  • dartfmt: This is the code formatting tool

  • docgen: This is the documentation generator tool

How to do it...

  1. For every tool, it might be useful to know or check its version. This is done with the -- version option such as dart --version with a typical output of Dart VM version: 1.3.0 (Tue Apr 08 09:06:23 2014) on "windows_ia32".

  2. 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.

  3. 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)

  4. 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 the test_pub app), use the following command in the folder where pubspec.yaml lives, pub get, with a typical output as follows:

    Resolving dependencies... (6.6s)
    Got dependencies!
  5. 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 the pubspec.lock file, so that your app can only use these versions.

  6. 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!

  7. 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 https://www.dartlang.org/articles/style-guide/.You can also use it from the command line, but then the default operation mode is cleaning up whitespace. Use the –t option to apply code transforms such as dartfmt -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/