English 中文(简体)
Dart Programming - Environment
  • 时间:2024-12-22

Dart Programming - Environment


Previous Page Next Page  

This chapter discusses setting up the execution environment for Dart on the Windows platform.

Executing Script Onpne with DartPad

You may test your scripts onpne by using the onpne editor at https://dartpad.dartlang.org/. The Dart Editor executes the script and displays both HTML as well as console output. The onpne editor is shipped with a set of preset code samples.

A screenshot of the Dartpad editor is given below −

Dartpad

Dartpad also enables to code in a more restrictive fashion. This can be achieved by checking the Strong mode option on the bottom right of the editor. Strong mode helps with −

    Stronger static and dynamic checking

    Idiomatic JavaScript code generation for better interoperabipty.

You may try the following example using Dartpad

void main() { 
   print( hello world ); 
}

The code will display the following output

hello world

Setting Up the Local Environment

In this section, let us see how to set up the local environment.

Using the Text Editor

Examples of a few editors include Windows Notepad, Notepad++, Emacs, vim or vi, etc. Editors may vary from one Operating System to another. The source files are typically named with the extension ".dart".

Instalpng the Dart SDK

The current stable version of Dart is 1.21.0. The dart sdk can be downloaded from −

A screenshot of the Dart SDK installation is given below −

Dart Installation

On completion of the SDK installation, set the PATH environment variable to −

<dart-sdk-path>in 

Verifying the Installation

To verify if Dart has been successfully installed, open the command prompt and enter the following command −

Dart 

If installation is successful, it will show the dart runtime.

IDE Support

A plethora of IDEs support scripting in Dart. Examples include Ecppse, IntelpJ, and WebStorm from Jet brains.

Given below are the steps for configuring the Dart environment using WebStrom IDE.

Instalpng WebStorm

The installation file for WebStorm can be downloaded from https://www.jetbrains.com/webstorm/download/#section=windows-version.

The WebStorm installation file is available for Mac OS, Windows and Linux.

After downloading the installation files, follow the steps given below −

    Install the Dart SDK: Refer to the steps psted above

    Create a new Dart project and configure Dart support

    To create a new Dart project,

      Cpck Create New Project from the Welcome Screen

      In the next dialog box, cpck Dart

    If there is no value specified for the Dart SDK path, then provide the SDK path. For example, the SDK path may be <dart installation directory>/dart/dartsdk.

Add a Dart File to the Project

To add a Dart file to the Project −

    Right-cpck on the Project

    New → Dart File

    Enter the name of the Dart Script

A screenshot of the WebStorm Editor is given below −

Dart File

The dart2js Tool

The dart2js tool compiles Dart code to JavaScript. Compipng Dart code to JS enables running the Dart script on browsers that do not support the Dart VM.

The dart2js tool is shipped as a part of the Dart SDK and can be found in the /dartsdk/bin folder.

To compile Dart to JavaScript, type the following command in the terminal

dart2js - - out = <output_file>.js  <dart_script>.dart

This command produces a file that contains the JavaScript equivalent of your Dart code. A complete tutorial on using this utipty can be found on the official Dart website.

Advertisements