Introduction to Dart for Java Developers-Part1
This tutorial gives a core overview of the programming language Dart
Dart is an open-source general-purpose programming language. It is originally developed by Google and later approved as a standard by ECMA. Dart is a new programming language meant for the server as well as the browser. Introduced by Google, the Dart SDK ships with its compiler — the Dart VM. The SDK also includes a utility -dart2js, a transpiler that generates JavaScript equivalent of a Dart Script. This tutorial provides a basic level understanding of the Dart programming language.
Audience
This tutorial will be quite helpful for all those developers who want to develop single-page web applications, a mobile app (for Flutter) a using Dart. It is meant for programmers with a stronghold on object-oriented concepts.
Prerequisites
The tutorial assumes that readers have adequate exposure to object-oriented programming concepts. If you have worked on Java, then it will help you further to grasp the concepts of Dart quickly. Don’t worry if you don’t have any programming knowledge experience still you can grasp the concepts of Dart quickly.
What you’ll learn
- Installation
- How to create constructors
- Different ways to specify parameters
- When and how to create getters and setters
- How Dart handles privacy
- How to create factories
- How functional programming works in Dart
- Other core Dart concepts
Executing Script Online with DartPad
You may test your scripts online by using the online editor at https://dartpad.dartlang.org/. The Dart Editor executes the script and displays both HTML as well as console output. The online editor is shipped with a set of preset code samples.
A screenshot of the Dartpad editor is given below −
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. 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”.
Installing the Dart SDK
The current stable version of Dart is 2.4.0. The dart SDK can be downloaded from −
A screenshot of the Dart SDK installation is given below −
On completion of the SDK installation, set the PATH environment variable to
<dart-sdk-path>\bin
Verifying the Installation
To verify if Dart has been successfully installed, open the command prompt and enter the following command −
Dart
If the installation is successful, it will show the dart runtime.
IDE Support
Examples include Eclipse, IntelliJ, and WebStormfrom Jet brains. Given below are the steps for configuring the Dart environment using WebStrom IDE.
Installing 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 listed above
- Create a new Dart project and configure Dart support
- To create a new Dart project,
- Click Create New Project from the Welcome Screen
- In the next dialog box, click 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-click on the Project
- New → Dart File
- Enter the name of the Dart file
A screenshot of the WebStorm Editor is given below −