Book Image

Dart By Example

By : David Mitchell
Book Image

Dart By Example

By: David Mitchell

Overview of this book

Table of Contents (17 chapters)
Dart By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Downloading the tools


Let's get started by installing a complete Dart development environment on your computer. The home of Dart on the Internet is https://www.dartlang.org, which contains the software, a wealth of documentation, and links to the online Dart community.

I would strongly recommend signing up for the e-mail lists on this site to keep up with Dart, and also the Dartisans Google+ community (http://g.co/dartisans). The number of daily messages can be overwhelming at first, but there are some great discussions and information sharing. It is also a great opportunity to interact with Dart's creators.

Tip

It is recommended that you use the Stable channel. This version updates roughly every few months via a built-in update tool. There is also a Dev (development) channel that updates once or twice a week with the very latest code from the Dart developers. This is great for seeing new features early, but it is not as polished or reliable as the Stable releases.

The downloads for all supported platforms (Linux, Mac, and Windows) are available at https://www.dartlang.org/tools/download.html. The same location provides information regarding building from source. Ensure that you download both the SDK and Dartium (a specialist browser).

It is recommended that Mac users use the Homebrew system to manage the installation and updates of Dart. Linux users can use a .deb if that format is supported on their distribution, with ZIP archives being the alternative. Windows versions are also supplied as ZIP archives.

Windows users can download, install, and update the SDK and Dartium using the Chocolatey installation system that is built on nuget and Powershell.

Note

See http://chocolatey.org for more information.

The Dart packages are called dart-sdk and dartium.

Introducing the WebStorm IDE

WebStorm is the premiere development environment for Dart and was created by JetBrains, which also publishes IntelliJ IDEA for Java and ReSharper for .Net.

WebStorm is a commercial package available at https://www.jetbrains.com/. There are free licenses available for many and deep discounts for individuals. It supports a range of languages and development platforms and Dart support is provided via a plug-in. The plugin comes pre-installed with the IDE.

Download the installation file and follow the installation steps on screen:

WebStorm has a number of powerful features to help write our web applications:

  • Auto-completion of code: properties and methods are listed as you type

  • A static analyzer that scans as you type for possible problems

  • Refactoring tools to help rename entities and extract methods

  • Quick fixes to correct common errors easily

  • Navigation via a code tree and finding usages of a function

  • A code reformatter to keep the source code in a tidy format

  • A powerful debugger

Alternative development environments

If you wish to use another IDE, there are plugins for IntelliJ IDEA and Eclipse. If you prefer a simple text editor, there are plugins for Sublime, Emacs, and Vim. These are all listed on the DartLang download page.

If you are an avid Visual Studio user, there is a community project underway to bring Dart to that environment. DartVS can be downloaded from the Visual Studio Gallery. It directly uses the DartAnalyzer from the SDK to report errors, warnings, and hints inside the IDE. Support for intellisense and other features is underway, too.

The Atom Editor (https://atom.io/) is an alternative for those who prefer a lightweight editor to a fully fledged development environment. The plugin, named dartlang (https://atom.io/packages/dartlang) provides code-completion, syntax highlighting, and access to some pub commands.

Help starting a project

To help create your new project, Dart has a tool called Stagehand that creates the scaffolding from a high quality project template of your choice. It takes care of common tasks such as folder structure, common libraries, and style sheets. It even has its own website (http://stagehand.pub/).

In WebStorm, creating a new project presents you with a range of powerful options, allowing you to set the locations of the Dart SDK and choose a project type for sample content. The project type gives a range of skeleton applications and is powered by the Stagehand application that is part of the SDK:

Stagehand can also be run from the command line, with the first step being to install it as a command:

pub global activate stagehand

This step installs the stagehand command from the stagehand package:

mkdir aproject
cd aproject
stagehand web-full

This will create a folder of sample content based on the web-full template.

Note

For full details of the current templates, see

https://pub.dartlang.org/packages/stagehand.

Elsewhere in the SDK

The download also includes a set of command-line tools, which can be found under the dart-sdk/bin path.

Tool

Description

dart

The standalone Dart Virtual Machine itself.

dart2js

The tool that converts Dart into JavaScript so that it can run on any modern browser.

Dartanalyzer

Analyzes the Dart source code for errors and suggestions. This is also used by development environments to provide feedback to users.

Dartdocgen

The documentation generator that works directly on Dart code.

Dartfmt

The intelligent code formatter that makes your code tidy and consistent.

pub

The package management and application deployment tool.

Tip

You may wish to place these tools on the system PATH so that you can call them from anywhere on the command line. This is optional as WebStorm provides an easy interface to these tools; however, many developers find it essential to a good workflow.

For more detailed information on the installation and configuration of pub, see

https://www.dartlang.org/tools/pub/installing.html

Also in this location is a snapshots folder. Most of the tools are written in Dart itself; when they are started up and initialized, a binary image of the initialized state can be stored and used for future executions of the program.

The snapshots are generated using the Dart executable and are mostly intended for frequently used command-line applications in deployment.