Book Image

Learning Dart, Second Edition - Second Edition

By : Ivo Balbaert
Book Image

Learning Dart, Second Edition - Second Edition

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Learning Dart Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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. Another awesome editor is WebStorm from JetBrains (see https://www.dartlang.org/tools/webstorm/).

Installing the Dart Editor

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

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

Then, go to http://www.dartlang.org/ and click on the GET STARTED and Download Dart menu item. Under Download the SDK, 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 Chromium to test your Dart apps) or download the Dart source code.

You are prompted to save a file named darteditor-os-nn.zip, where OS can be Windows, 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 smoothly, but if you encounter a problem, please review http://www.dartlang.org/docs/editor/getting-started/.

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 http://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. There could be another problem related to JRE. After unzipping a Dart Editor file, it is recommended to copy the jre folder from the Java folder to the dart folder (where the DartEditor.exe file is located). Again, it is important to copy the correct bit width version. If the 64-bit version of Dart Editor is used, the 64-bit version of JRE must be copied. This version is in the Program Files/Java folder under the jre7 name (don't copy the jre7 folder from Program Files (x86) / Java, this is the 32-bit version). Finally, after the jre7 folder is copied, it must be renamed from jre7 to jre.

Other options for working with Dart code

If you are more familiar working in the Eclipse environment, you can simply use the Dart Eclipse plugin (https://www.dartlang.org/tools/eclipse-plugin/), which provides the same functionality as Dart Editor. Another rich working environment alternative is built in the WebStorm environment from IntelliJ: https://www.dartlang.org/tools/webstorm/.

If you need a cloud environment, you can use DartPad for simpler projects: https://dartpad.dartlang.org/.

Your first Dart program

Double-click on DartEditor.exe to open the editor. Navigate to File | New Project or click on the first button below the menu (Create a new Dart Project...). Fill in an application name (for example, dart1) and choose the folder 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). We will leave the Console application – A simple command-line application selection.

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

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

Tip

Downloading the example code

You can download the example code files for all the 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 void keyword indicates (as in Java or C#) that the method does not explicitly return an object (indeed print only produces output to the console), but the 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):

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 exit code=0 message meaning that all went well.

The Files and Apps tabs are useful for browsing through your applications and for creating, copying, moving, renaming, and deleting files. The Outline tab 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 dart1 folder and executed the dart main.dart command to produce the same output as shown in the following screenshot:

A Dart console application

Note

To let this work, you must first let the OS know where to find the Dart V. 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.