Learning Dart
上QQ阅读APP看书,第一时间看更新

Getting started with Dart

The Dart project team wants us to get an optimal development experience, so they provide a full but lightweight IDE: the Dart Editor, a light version of the well-known Eclipse environment. Installing this is the easiest way to get started, because it comprises the full Dart environment.

Installing the Dart Editor

Because Eclipse is written in Java, we need a Java Runtime Environment or JRE (Version 6 or greater) on our system (this is not needed for Dart itself, only for the Dart Editor). To check if this is already the case, go to http://www.java.com/en/download/installed.jsp.

If it is not the case, head for http://www.oracle.com/technetwork/java/javase/downloads/index.html, and click on the JRE DOWNLOAD button, choose the JRE for your platform and then click on Run to start the installation.

Then go to https://www.dartlang.org/ and click on the appropriate Download Dart button and under "Everything you need" choose the appropriate button (according to whether your installed OS is 32 bit or 64 bit) to download the editor.

Tip

This is also the page where you can download the SDK separately, or the Dartium browser (a version of Chrome to test your Dart apps) or download the Dart source code.

You are prompted to save a file named darteditor-os-xnn.zip, where os can be win32, linux, or macos, and nn is 32 or 64. Extracting the content of this file will create a folder named dart containing everything you need: dart-sdk, dartium, and DartEditor. This procedure should go smooth but if you encounter a problem, please review https://www.dartlang.org/tools/editor/troubleshoot.html.

Note

In case you get the following error message: Failed to load the JNI shared library C:\Program Files(x86)\Java\jre6\\bin\client\jvm.dll, do not worry. This happens when JRE and Dart Editor do not have the same bit width. More precisely, this happens when you go to www.java.com to download JRE. In order to be sure what JRE to select, it is safer to go to http://www.oracle.com/technetwork/java/javase/downloads/index.html, click on the JRE DOWNLOAD button and choose the appropriate version. If possible, use a 64-bit versions of JRE and Dart Editor.

Your first Dart program

Double-click on DartEditor.exe to open the editor. Navigate to File | New Application or click on the first button below the menu (Create a new Dart Application...). Fill in an application name (for example, dart1) and choose the folder where you want the code file to be created (make a folder such as dart_apps to provide some structure; you can do this while using the Browse button). Select Command-line application.

With these names a folder dart1 is made as a subfolder of dart_apps, and a source-file dart1.dart is created in dart1\bin with the following code (we'll explain the pubspec.yaml and the packages folder in one of the following examples):

void main() {
  print("Hello, World!");
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Here we see immediately that Dart is a C-syntax style language, using { } to surround code and ; to terminate statements. Every app also has a unique main() function, which is the starting point of the application.

This is probably the shortest app possible, but it can be even shorter! The keyword void indicates (as in Java or C#) that the method does not explicitly return an object (indeed a print only produces output to the console), but return types can be left out. Furthermore, when a function has only one single expression, we can shorten this further to the following elegant shorthand syntax:

main() => print("Hello, World!");

Now, change the printed string to "Becoming a Dart Ninja!" and click on the green arrow button (or press Ctrl + R) to run the application. You should see something like the following screenshot (where the Files, Apps, and Outline items from the Tools menu were selected):

Your first Dart program

The Dart Editor

You have successfully executed your first Dart program!

Near the bottom of the screen we see our string printed out, together with the message exit code=0 meaning all went well.

The Files tab is useful for browsing through your applications, and for creating, copying, moving, renaming, and deleting files. The Outline tab (available via Tools | Outline) now only shows main(), but this tool will quickly become very useful because it provides an overview of the code in the active file.

Because this was a command-line application, we could just as easily have opened a console in our folder dart1 and executed the command: dart dart1.dart to produce the same output as shown in the following screenshot:

Your first Dart program

A Dart console application

Note

To let this work, you must first let the OS know where to find the dart VM; so, for example, in Windows you change the PATH environment variable to include C:\dart\dart-sdk\bin, if your Dart installation lives in C:\dart.